-
-
Save vuchau/f47f338201a0f343c7b4c6dd70e9735a to your computer and use it in GitHub Desktop.
Flutter AWS Cognito Federated Identities Login
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
import 'package:amazon_cognito_identity_dart/cognito.dart'; | |
import 'package:amazon_cognito_identity_dart/sig_v4.dart'; | |
import 'package:http/http.dart' as http; | |
class Credentials { | |
final CognitoCredentials _cognitoCredentials; | |
final String _token; | |
final String _authenticator; | |
Credentials(String identityPoolId, String userPoolId, String clientId, this._token, [this._authenticator]) | |
: _cognitoCredentials = new CognitoCredentials(identityPoolId, new CognitoUserPool(userPoolId, clientId)); | |
Future<CognitoCredentials> get cognitoCredentials async { | |
await _cognitoCredentials.getAwsCredentials(_token, _authenticator); | |
return _cognitoCredentials; | |
} | |
} | |
class Api { | |
final String endpoint; | |
final String path; | |
final String region; | |
final Credentials credentials; | |
Api(this.endpoint, this.path, this.region, this.credentials); | |
post(Map body) async { | |
CognitoCredentials cognitoCredentials = await credentials.cognitoCredentials; | |
final awsSigV4Client = new AwsSigV4Client( | |
cognitoCredentials.accessKeyId, | |
cognitoCredentials.secretAccessKey, | |
endpoint, | |
sessionToken: cognitoCredentials.sessionToken, | |
region: region, | |
); | |
final signedRequest = new SigV4Request( | |
awsSigV4Client, | |
method: 'POST', | |
path: path, | |
// headers: new Map<String, String>.from({'header-1': 'one', 'header-2': 'two'}), | |
// queryParams: new Map<String, String>.from({'tracking': 'x123'}), | |
body: new Map<String, dynamic>.from(body), | |
); | |
http.Response response; | |
response = await http.post(signedRequest.url, headers: signedRequest.headers, body: signedRequest.body); | |
return response; | |
} | |
} |
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
import 'package:flutter_facebook_login/flutter_facebook_login.dart'; | |
Future<FacebookLoginResult> signInWithFacebook() async { | |
final facebookLogin = FacebookLogin(); | |
final facebookLoginResult = await facebookLogin.logInWithReadPermissions(['email']); | |
return facebookLoginResult; | |
} |
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
import 'package:google_sign_in/google_sign_in.dart'; | |
Future<GoogleSignInAuthentication> signInWithGoogle() async { | |
final GoogleSignIn googleSignIn = GoogleSignIn(scopes: ['email']); | |
final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn(); | |
GoogleSignInAuthentication googleSignInAuthentication = await googleSignInAccount.authentication; | |
return googleSignInAuthentication; | |
} |
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
import 'package:flutter/material.dart'; | |
import 'package:flutter_app/aws.dart'; | |
import 'package:flutter_app/facebook.dart'; | |
import 'package:flutter_app/google.dart'; | |
import 'package:flutter_app/secret.dart'; | |
void main() => runApp(MyApp()); | |
signInFacebook() async { | |
final facebookLoginResult = await signInWithFacebook(); | |
final credentials = new Credentials( | |
cognitoIdentityPoolId, | |
cognitoUserPoolId, | |
cognitoClientId, | |
facebookLoginResult.accessToken.token, | |
'graph.facebook.com', | |
); | |
final api = Api(apiEndpointUrl, '/flutter', 'ap-southeast-2', credentials); | |
final result = await api.post({}); | |
print(result.body); | |
} | |
signInGoogle() async { | |
final googleSignInAuthentication = await signInWithGoogle(); | |
final credentials = new Credentials( | |
cognitoIdentityPoolId, | |
cognitoUserPoolId, | |
cognitoClientId, | |
googleSignInAuthentication.idToken, | |
'accounts.google.com', | |
); | |
final api = Api(apiEndpointUrl, '/flutter', 'ap-southeast-2', credentials); | |
final result = await api.post({}); | |
print(result.body); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment