Skip to content

Instantly share code, notes, and snippets.

View DmitrySikorsky's full-sized avatar

Dmitry Sikorsky DmitrySikorsky

View GitHub Profile
Future main() async {
...
String? token;
if (kIsWeb) {
token = Uri.base.queryParameters['token'];
}
runApp(MyApp(token: token));
Future main() async {
...
String? token;
if (kIsWeb) {
token = Uri.base.queryParameters['token'];
}
runApp(MyApp(token: token));
public async Task<ActionResult> IndexAsync(string id_token, string state)
{
if (string.IsNullOrEmpty(id_token) || string.IsNullOrEmpty(state) || !state.Contains(';'))
return this.BadRequest();
string platform = state.Split(';')[0];
if (!Guid.TryParse(state.Split(';')[1], out Guid emailValidationTokenId))
return this.BadRequest();
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index(string platform, string provider, string token)
{
string url;
if (provider == "apple")
url = "https://appleid.apple.com/auth/authorize" +
"?client_id=com.your.app" +
Future validateEmailWithOAuth2AndLogin(LoginMethod loginMethod) async {
// Creates an empty email validation token on server-side so we have its ID
EmailValidationToken emailValidationToken = await beginOAuth2EmailValidation(loginMethod);
String url = 'https://auth.youapp.com/?${kIsWeb ? 'platform=web&' : ''}provider=${loginMethod.name}&token=${emailValidationToken.id}';
if (await canLaunchUrlString(url)) {
// Open the URL in a new browser window on all the platforms except web
await launchUrlString(
url,
private async Task<string> GetEmailFromGoogleCode(string googleCode)
{
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets()
{
ClientId = "XXXXXXXXXX.apps.googleusercontent.com",
ClientSecret = "XXXXXXXXXX"
},
private async Task<string> GetEmailFromAppleCode(string appleCode)
{
string privateKey = System.IO.File.ReadAllText(Path.Combine(this.environment.ContentRootPath, "AuthKey_XXXXXXXXXX.p8"));
AppleAuthProvider provider = new AppleAuthProvider(
"com.your.app",
"XXXXXXXXXX",
"XXXXXXXXXX",
"https://auth.yourapp.com/callback", // I don't know why it must be here but it is not used
string.Empty
);
if (!kIsWeb && Platform.isAndroid) {
GoogleSignIn googleSignIn = GoogleSignIn(
scopes: <String>['email'],
);
try {
GoogleSignInAccount account = (await googleSignIn.signIn())!;
// Send the account.serverAuthCode to your server to exchange it for the token
await validateEmailWithGoogleAndLogin(account.serverAuthCode!);
if (!kIsWeb && (Platform.isIOS || Platform.isMacOS)) {
AuthorizationCredentialAppleID credential = await SignInWithApple.getAppleIDCredential(
scopes: [AppleIDAuthorizationScopes.email],
);
// Send the credential.authorizationCode to your server to exchange it for the token
await validateEmailWithAppleAndLogin(credential.authorizationCode);
}
public override void OnActionExecuting(ActionExecutingContext actionExecutingContext)
{
base.OnActionExecuting(actionExecutingContext);
this.HandleViewModelMultilingualProperties(actionExecutingContext);
}
private void HandleViewModelMultilingualProperties(ActionExecutingContext actionExecutingContext)
{
ViewModelBase viewModel = this.GetViewModelFromActionExecutingContext(actionExecutingContext);