Skip to content

Commit

Permalink
add social login common
Browse files Browse the repository at this point in the history
  • Loading branch information
ngocle2497 committed Feb 10, 2022
1 parent 616004f commit 3f9398b
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 0 deletions.
5 changes: 5 additions & 0 deletions template/src/app/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ export * from './native-module';
* Firebase
*/
// export * from './firebase';

/**
* Social login
*/
// export * from './social-login'
105 changes: 105 additions & 0 deletions template/src/app/common/social-login/apple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// import {Platform} from 'react-native';
// import {
// appleAuth,
// appleAuthAndroid,
// } from '@invertase/react-native-apple-authentication';
// import jwt_decode from 'jwt-decode';
// import {loadString, saveString} from '@utils';
export {};
// import {randomUniqueId} from '../string/index';

// async function pickInfoAppleResponse(info: any = {}, appleId: string) {
// try {
// const infoCache = await loadString(appleId);

// const infoName = infoCache ? infoCache : info;
// let userFullName = `${infoName?.familyName?.trim?.() || ''}${
// infoName?.namePrefix || ''
// } ${infoName?.middleName || ''} ${infoName?.givenName || ''}`?.trim();

// if (!userFullName) {
// userFullName = `${infoName?.firstName || ''} ${
// infoName?.lastName || ''
// }`.trim();
// }

// if (!Object.keys(infoCache || {})?.length) {
// saveString(appleId, JSON.stringify(info));
// }

// return userFullName;
// } catch {
// return undefined;
// }
// }

// export const AppleService = {
// appleSignin: async () => {
// if (Platform.OS === 'ios') {
// try {
// const appleAuthRequestResponse = await appleAuth.performRequest({
// requestedOperation: appleAuth.Operation.LOGIN,
// requestedScopes: [appleAuth.Scope.EMAIL, appleAuth.Scope.FULL_NAME],
// });

// const userFullName = await pickInfoAppleResponse(
// appleAuthRequestResponse.fullName,
// appleAuthRequestResponse.user,
// );

// return {...appleAuthRequestResponse, userFullName};
// } catch (err) {
// console.log('IOS-APPPLE-LOGIN-ERROR', err);
// }
// } else {
// try {
// // Generate secure, random values for state and nonce
// const rawNonce = randomUniqueId();
// const state = randomUniqueId();

// // Configure the request
// appleAuthAndroid.configure({
// // The Service ID you registered with Apple
// clientId: 'CLIENT_ID_APPLE_SERVICE',

// // Return URL added to your Apple dev console. We intercept this redirect, but it must still match
// // the URL you provided to Apple. It can be an empty route on your backend as it's never called.
// redirectUri: 'APPLE_REDIRECT_URI',

// // The type of response requested - code, id_token, or both.
// responseType: appleAuthAndroid.ResponseType.ALL,

// // The amount of user information requested from Apple.
// scope: appleAuthAndroid.Scope.ALL,

// // Random nonce value that will be SHA256 hashed before sending to Apple.
// nonce: rawNonce,

// // Unique state value used to prevent CSRF attacks. A UUID will be generated if nothing is provided.
// state,
// });

// // Open the browser window for user sign in
// const response = await appleAuthAndroid.signIn();
// const infoLogin = {
// ...response,
// ...(jwt_decode(response.id_token || '') as any),
// };

// const appleId = infoLogin.sub;
// const {nonce} = infoLogin;
// const identityToken = response.id_token || '';
// // apple return full name one times. so save full name util push to BE successfully
// const userFullName = await pickInfoAppleResponse(
// response.user?.name,
// appleId,
// );

// return {user: appleId, nonce, identityToken, userFullName};
// // Send the authorization code to your backend for verification
// } catch (err) {
// console.log('ANDROID-APPPLE-LOGIN-ERROR', err);
// }
// }
// },
// };
70 changes: 70 additions & 0 deletions template/src/app/common/social-login/facebook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// import {
// AccessToken,
// GraphRequest,
// GraphRequestManager,
// LoginManager,
// Settings,
// } from 'react-native-fbsdk-next';
export {};
// const init = () => {
// Settings.initializeSDK();
// };

// type LoginResult = {success: boolean; token?: string};
// const login = () => {
// return new Promise<LoginResult>(rs => {
// LoginManager.logInWithPermissions(['public_profile', 'email']).then(
// result => {
// if (result.isCancelled) {
// rs({success: false});
// } else {
// AccessToken.getCurrentAccessToken()
// .then(data => {
// if (data && data.accessToken) {
// rs({success: true, token: data.accessToken});
// } else {
// rs({success: false});
// }
// })
// .catch(err => {
// console.log('FACEBOOK-LOGIN-ERROR', err);
// rs({success: false});
// });
// }
// },
// );
// });
// };

// const logout = async () => {
// try {
// const data = await AccessToken.getCurrentAccessToken();
// if (data?.accessToken) {
// const logoutGraph = new GraphRequest(
// 'me/permissions/',
// {
// accessToken: data?.accessToken,
// httpMethod: 'DELETE',
// },
// error => {
// if (error) {
// console.log('Error fetching data: ' + error.toString());
// } else {
// LoginManager.logOut();
// }
// },
// );
// new GraphRequestManager().addRequest(logoutGraph).start();
// } else {
// LoginManager.logOut();
// }
// } catch (err) {
// console.log('FACEBOOK-LOGOUT-ERROR', err);
// }
// };

// export const FacebookService = {
// init,
// login,
// logout,
// };
33 changes: 33 additions & 0 deletions template/src/app/common/social-login/google.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// import {GoogleSignin} from '@react-native-google-signin/google-signin';

// const init = () => {
// GoogleSignin.configure();
// };
export {};
// type LoginResult = {success: boolean; token?: string};
// const login = async (): Promise<LoginResult> => {
// try {
// await GoogleSignin.hasPlayServices();
// await GoogleSignin.signIn();
// const userToken = await GoogleSignin.getTokens();
// return {success: true, token: userToken.accessToken};
// } catch (err) {
// console.log('GOOGLE-LOGIN-ERROR', err);
// return {success: false};
// }
// };

// const logout = async () => {
// try {
// await GoogleSignin.signOut();
// return true;
// } catch (err) {
// console.log('GOOGLE-LOGOUT-ERROR', err);
// }
// };

// export const GoogleService = {
// init,
// login,
// logout,
// };
3 changes: 3 additions & 0 deletions template/src/app/common/social-login/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './apple';
export * from './facebook';
export * from './google';

0 comments on commit 3f9398b

Please sign in to comment.