Skip to content

Commit

Permalink
Merge pull request #79 from melton-foundation/pranav_beta_fixes
Browse files Browse the repository at this point in the history
BREAKING: feat(oauth): upgrade to new /login api to support Apple Sign in on app reinstall, cleanup
  • Loading branch information
pranavsb authored Nov 11, 2020
2 parents 163c276 + 282d330 commit fdfd1bd
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
27 changes: 24 additions & 3 deletions lib/api/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,33 @@ class ApiService {
"Content-Type": "application/json"
};

Future<UserRegistrationStatusModel> getAppToken(
String email, String oauthToken, String oauthProvider) async {
Future<UserRegistrationStatusModel> getAppTokenUsingGoogleOauth(
String email, String oauthToken) async {
Map<String, String> jsonBodyMap = {
"appleId": null,
"email": email,
"token": oauthToken,
"authProvider": oauthProvider
"authProvider": "GOOGLE"
};
http.Response response = await http.post(apiUrl + "login/",
headers: contentHeader, body: json.encode(jsonBodyMap));
if (response.statusCode == 200) {
return UserRegistrationStatusModel.fromJson(
json.decode(utf8.decode(response.bodyBytes)));
} else if (response.statusCode == 403) {
return UserRegistrationStatusModel(isApproved: false, appToken: null);
} else {
return null;
}
}

Future<UserRegistrationStatusModel> getAppTokenUsingAppleOauth(
String appleUserId, String email, String oauthToken) async {
Map<String, String> jsonBodyMap = {
"appleId": appleUserId,
"email": email,
"token": oauthToken,
"authProvider": "APPLE"
};
http.Response response = await http.post(apiUrl + "login/",
headers: contentHeader, body: json.encode(jsonBodyMap));
Expand Down
5 changes: 3 additions & 2 deletions lib/screens/authorization_wall.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ class AuthorizationWall extends StatelessWidget {
children: [
Image.asset("assets/errors/error_user_not_found.png"),
FormTitle("WHO ARE YOU?"),
FormSubtitle("Your email wasn't saved in a Melton database."),
FormSubtitle(
"We will verify that you're a Melton Fellow, and approve you."),
"Your email wasn't saved in a Melton database. Have you signed-up?"),
FormSubtitle(
"We will verify that you're a Melton Fellow and approve you."),
FormSubtitle(
"Try using another email, or signing in again later :)"),
Padding(padding: EdgeInsets.fromLTRB(20, 20, 20, 20)),
Expand Down
20 changes: 6 additions & 14 deletions lib/screens/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import 'package:melton_app/util/text_util.dart';
import 'package:melton_app/util/token_handler.dart';
import 'package:melton_app/screens/components/sign_up.dart';
import 'package:melton_app/screens/splash.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:sign_in_with_apple/sign_in_with_apple.dart';
import 'package:melton_app/util/url_launch_util.dart';

Expand Down Expand Up @@ -59,7 +58,7 @@ class _LoginScreenState extends State<LoginScreen> {
fontWeight: FontWeight.bold,
color: Constants.meltonBlue)),
onTap: () {
launchUrlWebview("https://meltonapp.com/privacy/");
launchUrlWebview(Constants.MELTON_PRIVACY_POLICY_URL);
},
),
CheckboxListTile(
Expand Down Expand Up @@ -170,17 +169,10 @@ class _LoginScreenState extends State<LoginScreen> {
AppleIDAuthorizationScopes.fullName,
],
);
String appleEmail;
SharedPreferences preferences = await SharedPreferences.getInstance();
if (credential.email == null) {
appleEmail = preferences.getString(TokenHandler.APPLE_EMAIL_KEY);
} else {
await preferences.setString(
TokenHandler.APPLE_EMAIL_KEY, credential.email);
appleEmail = credential.email;
}
tokenOrUnauthorized = await ApiService()
.getAppToken(appleEmail, credential.authorizationCode, "APPLE");
tokenOrUnauthorized = await ApiService().getAppTokenUsingAppleOauth(
credential.userIdentifier,
credential.email,
credential.authorizationCode);
}
if (tokenOrUnauthorized?.appToken != null) {
PersistentStorage storage = GetIt.instance.get<PersistentStorage>();
Expand All @@ -207,7 +199,7 @@ class _LoginScreenState extends State<LoginScreen> {
await _googleSignIn.signIn().then((result) async {
await result.authentication.then((googleKey) async {
tokenOrUnauthorized = await ApiService()
.getAppToken(result.email, googleKey.idToken, "GOOGLE");
.getAppTokenUsingGoogleOauth(result.email, googleKey.idToken);
}).catchError((err) {
//todo error screen
});
Expand Down
1 change: 0 additions & 1 deletion lib/util/token_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:melton_app/util/persistent_storage.dart';
class TokenHandler {
String token;
static const String APP_TOKEN_KEY = "appToken";
static const String APPLE_EMAIL_KEY = "appleEmail";

Future<void> refresh(PersistentStorage storage) async {
token = await storage.readStringFromStorage(APP_TOKEN_KEY);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: App for Melton Foundation Fellows - https://meltonapp.com/

publish_to: 'none'

version: 1.0.1+2
version: 1.1.0+3

environment:
sdk: ">=2.7.0 <3.0.0"
Expand Down

0 comments on commit fdfd1bd

Please sign in to comment.