Fakultas Ilmu Komputer UI

Commit 0f8353b4 authored by Zafir Rasyidi Taufik's avatar Zafir Rasyidi Taufik
Browse files

[GREEN]Added implementation to check if user already exists

parent 96614cbd
Pipeline #79301 failed with stage
in 53 minutes and 22 seconds
......@@ -40,21 +40,56 @@ const LoginPage = () => {
});
const [userInfo, setUserInfo] = useState<any>();
const signIn = async () => {
const checkIfTokenHasCorrespondingUser = async (idToken: string | null) => {
setShouldLoading(true)
const checkRes = await services.main.checkGoogleUserExists({
token: idToken
});
setShouldLoading(false)
if (checkRes === undefined) {
setAlert({
illustration: WonderingIllustration,
message: LoginResponse.NoInternetConn,
})
} else {
if (checkRes.status === 200) {
LocalStorage.setItem('token', checkRes.data.token);
setToken(checkRes.data.token);
return false;
} else if (checkRes.status === 500) {
setAlert({
illustration: WonderingIllustration,
message: LoginResponse.UnknownServerError,
})
return false;
} else {
return true;
}
}
}
const signInWithGoogle = async () => {
try {
await GoogleSignin.hasPlayServices();
const tempUserInfo = await GoogleSignin.signIn();
setUserInfo(tempUserInfo);
navigation.navigate('officer-signup-form-google-signin', {idToken: tempUserInfo.idToken});
const noGoogleUser = await checkIfTokenHasCorrespondingUser(tempUserInfo.idToken);
if(noGoogleUser)
navigation.navigate('officer-signup-form-google-signin', {idToken: tempUserInfo.idToken});
} catch (error) {
console.log(error)
if (error.code === statusCodes.SIGN_IN_CANCELLED) {
// user cancelled the login flow
} else if (error.code === statusCodes.IN_PROGRESS) {
// operation (e.g. sign in) is in progress already
} else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
// play services not available or outdated
setAlert({
illustration: WonderingIllustration,
message: "Play Service sedang tidak tersedia, coba lagi nanti",
})
} else {
}
}
......@@ -180,7 +215,7 @@ const LoginPage = () => {
style={{ width: 192, height: 48 }}
size={GoogleSigninButton.Size.Wide}
color={GoogleSigninButton.Color.Dark}
onPress={() => signIn()}
onPress={() => signInWithGoogle()}
/>
</Box>
</Action>
......
......@@ -142,7 +142,7 @@ const OfficerSignupFormGoogleSignin = () => {
setIsLoading(true)
const response = await global.services.main.createUserGoogleSignin({
idToken: route.params.idToken,
token: route.params.idToken,
phone_number: form.fields.phone_number.value,
district: form.fields.district.value,
sub_district: form.fields.sub_district.value
......
......@@ -10,6 +10,7 @@ const END_POINTS = {
LIST_POSITIVE_CASES: createEndpoint(["cases", "positive-cases"]),
MONITORING_CASES: createEndpoint(["cases", "monitoring-cases"]),
LOGS: createEndpoint(["logs"]),
GOOGLE: createEndpoint(["google"]),
LOGIN: '/auth/token/',
};
......@@ -89,7 +90,7 @@ export default function useMainService(idToken: string) {
}
interface UserFormTypeGoogleSignin {
idToken: string
token: string
phone_number: string;
district: string;
sub_district: string;
......@@ -102,6 +103,17 @@ export default function useMainService(idToken: string) {
return fetchWithoutAuthentication(endPoint, Method.POST, body);
}
interface CheckGoogleTokenType {
token: string
}
async function checkGoogleUserExists(body: CheckGoogleTokenType) {
const endPoint = END_POINTS.GOOGLE([
null,
])
return fetchWithoutAuthentication(endPoint, Method.POST, body);
}
async function createCaseSubject(body: object) {
const endPoint = END_POINTS.CASE_SUBJECTS([
null,
......@@ -275,6 +287,7 @@ export default function useMainService(idToken: string) {
login,
createUser,
createUserGoogleSignin,
checkGoogleUserExists,
me,
// Contact Investigation Form
createCaseSubject,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment