-
-
Notifications
You must be signed in to change notification settings - Fork 17
Adding Social Logins #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GolfJimB
wants to merge
3
commits into
mitchelsellers:main
Choose a base branch
from
GolfJimB:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,33 @@ | ||
| using Microsoft.AspNetCore.Components.Authorization; | ||
| using Microsoft.AspNetCore.Components.WebView.Maui; | ||
| using SampleMauiApplication.Data; | ||
| using SampleMauiApplication.Services; | ||
|
|
||
| namespace SampleMauiApplication; | ||
|
|
||
| public static class MauiProgram | ||
| { | ||
| public static MauiApp CreateMauiApp() | ||
| public static string APIUrl = DeviceInfo.Platform == DevicePlatform.Android ? "http://10.0.2.2:5096/api/" : "https://localhost:44334/api/"; | ||
| public static MauiApp CreateMauiApp() | ||
| { | ||
| var builder = MauiApp.CreateBuilder(); | ||
| builder | ||
| .RegisterBlazorMauiWebView() | ||
| .UseMauiApp<App>() | ||
| .ConfigureFonts(fonts => | ||
| { | ||
| fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); | ||
| }); | ||
|
|
||
| //Register needed elements for authentication | ||
| builder.Services.AddAuthorizationCore(); // This is the core functionality | ||
| builder.Services.AddMauiBlazorWebView(); | ||
| //Register needed elements for authentication | ||
| builder.Services.AddAuthorizationCore(); // This is the core functionality | ||
| builder.Services.AddScoped<CustomAuthenticationStateProvider>(); // This is our custom provider | ||
| //When asking for the default Microsoft one, give ours! | ||
| builder.Services.AddScoped<AuthenticationStateProvider>(s => s.GetRequiredService<CustomAuthenticationStateProvider>()); | ||
|
|
||
| builder.Services.AddBlazorWebView(); | ||
| builder.Services.AddSingleton<WeatherForecastService>(); | ||
| builder.Services.AddScoped(_ => new AuthenticatingService(APIUrl)); | ||
|
|
||
| return builder.Build(); | ||
| return builder.Build(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| @page "/loginemail" | ||
| @using SampleMauiApplication.Models | ||
| @inject NavigationManager Navigation; | ||
| @inject CustomAuthenticationStateProvider AuthStateProvider; | ||
|
|
||
| <h3>Login to Access Application</h3> | ||
|
|
||
| <div class="alert alert-info"> | ||
| This is a dummy login page, providing `Test` for the Username and Password will authenticate you. | ||
| </div> | ||
|
|
||
| <EditForm Model="@loginModel" OnValidSubmit="@HandleValidSubmit"> | ||
| <DataAnnotationsValidator /> | ||
| <ValidationSummary /> | ||
| <div class="alert alert-danger" hidden="@loginModel.LoginFailureHidden"> | ||
| Invalid login attempt, please correct and try again. | ||
| </div> | ||
| <div class="form-group"> | ||
| <label>Username</label> | ||
| <InputText id="email" @bind-Value="loginModel.Username" class="form-control" /> | ||
| </div> | ||
| <div class="form-group"> | ||
| <label>Password</label> | ||
| <InputText id="password" type="password" @bind-Value="loginModel.Password" class="form-control" /> | ||
| </div> | ||
| <div class="form-group mt-1"> | ||
| <button type="submit" class="btn btn-primary w-100">Login Now</button> | ||
| </div> | ||
| </EditForm> | ||
|
|
||
| @code { | ||
| private LoginViewModel loginModel = new(); | ||
|
|
||
| private async Task HandleValidSubmit() | ||
| { | ||
| //Valiate user acount | ||
| var jwtToken = await loginModel.ValidateLoginAsync(); | ||
|
|
||
| //Not successful, don't need to do anything | ||
| if (String.IsNullOrEmpty(jwtToken)) | ||
| return; | ||
|
|
||
| //Call login and recirect | ||
| await AuthStateProvider.Login(jwtToken); | ||
| Navigation.NavigateTo(""); //Root URL | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 10 additions & 1 deletion
11
src/SampleMauiApplication/Platforms/Android/MainActivity.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,19 @@ | ||
| using Android.App; | ||
| using Android.Content.PM; | ||
| using Android.OS; | ||
| using Android.Content; | ||
|
|
||
| namespace SampleMauiApplication; | ||
|
|
||
| [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)] | ||
| public class MainActivity : MauiAppCompatActivity | ||
| { | ||
|
|
||
| [Activity(NoHistory = true, LaunchMode = LaunchMode.SingleTop, Exported = true)] | ||
| [IntentFilter( | ||
| new[] { Intent.ActionView }, | ||
| Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, | ||
| DataScheme = "myapp")] | ||
| public class WebAuthenticationCallbackActivity : Microsoft.Maui.Authentication.WebAuthenticatorCallbackActivity | ||
| { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/SampleMauiApplication/Services/AuthenticatingService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using System.Windows.Input; | ||
| using Microsoft.Maui; | ||
| using Microsoft.Maui.Authentication; | ||
| using Microsoft.Maui.Controls; | ||
| using Microsoft.Maui.Devices; | ||
|
|
||
| namespace SampleMauiApplication.Services | ||
| { | ||
| //From this example: https://docs.microsoft.com/en-us/dotnet/maui/platform-integration/communication/authentication?tabs=android | ||
| public class AuthenticatingService | ||
| { | ||
| public string authenticationUrl { get; set; } | ||
| public AuthenticatingService(string APIUrl) | ||
| { | ||
| //points to an api controller like this one: https://github.com/dotnet/maui/blob/main/src/Essentials/samples/Sample.Server.WebAuthenticator/Controllers/MobileAuthController.cs | ||
| authenticationUrl = APIUrl + "mobileauth/"; | ||
| } | ||
|
|
||
| public async Task<string> OnAuthenticate(string scheme) | ||
| { | ||
| try | ||
| { | ||
| WebAuthenticatorResult r = null; | ||
|
|
||
| if (scheme.Equals("Apple", StringComparison.Ordinal) | ||
| && DeviceInfo.Platform == DevicePlatform.iOS | ||
| && DeviceInfo.Version.Major >= 13) | ||
| { | ||
| // Make sure to enable Apple Sign In in both the | ||
| // entitlements and the provisioning profile. | ||
| var options = new AppleSignInAuthenticator.Options | ||
| { | ||
| IncludeEmailScope = true, | ||
| IncludeFullNameScope = true, | ||
| }; | ||
| r = await AppleSignInAuthenticator.AuthenticateAsync(options); | ||
| } | ||
| else | ||
| { | ||
| var authUrl = new Uri(authenticationUrl + scheme); | ||
| var callbackUrl = new Uri("myapp://"); | ||
|
|
||
| r = await WebAuthenticator.AuthenticateAsync(authUrl, callbackUrl); | ||
| } | ||
|
|
||
| var authToken = string.Empty; | ||
| if (r.Properties.TryGetValue("name", out var name) && !string.IsNullOrEmpty(name)) | ||
| authToken += $"Name: {name}{Environment.NewLine}"; | ||
| if (r.Properties.TryGetValue("email", out var email) && !string.IsNullOrEmpty(email)) | ||
| authToken += $"Email: {email}{Environment.NewLine}"; | ||
| authToken += r?.AccessToken ?? r?.IdToken; | ||
|
|
||
| return authToken; | ||
| } | ||
| catch (OperationCanceledException) | ||
| { | ||
| Console.WriteLine("Login canceled."); | ||
| return string.Empty; | ||
| //ToDo - handle fail | ||
| //await DisplayAlertAsync("Login canceled."); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Console.WriteLine($"Failed: {ex.Message}"); | ||
| return string.Empty; | ||
| //ToDo - handle fail | ||
| //await DisplayAlertAsync($"Failed: {ex.Message}"); | ||
| } | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this needed for social login? (Similar question to the Image Capture intent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah nope, just the CustomTabsService needed, my bad.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now worries, just want to try and keep this to "MVP" status, especially for permission/intent asks