Skip to content

Instantly share code, notes, and snippets.

@riyastir
Created July 10, 2021 19:07
Show Gist options
  • Save riyastir/c445303c3211b8842526bdbd169ba1e5 to your computer and use it in GitHub Desktop.
Save riyastir/c445303c3211b8842526bdbd169ba1e5 to your computer and use it in GitHub Desktop.
export const AuthContext = React.createContext();
const [state, dispatch] = React.useReducer(
(prevState, action) => {
switch (action.type) {
case "RESTORE_TOKEN":
return {
...prevState,
userToken: action.token,
isLoading: false,
};
case "SIGN_IN":
return {
...prevState,
isSignout: false,
userToken: action.token,
};
case "SIGN_OUT":
return {
...prevState,
isSignout: true,
userToken: null,
};
}
},
{
isLoading: true,
isSignout: false,
userToken: null,
}
);
dispatch({ type: "SIGN_IN", token: jsonLoginData.token });
const authContext = React.useMemo(
() => ({
signIn: async (data) => {
if (data.username == "" && data.password == "") {
setVerifyButton(false);
setModalMessage("Please enter your email and password");
modal1.current.open();
} else if (data.username == "" && data.password != "") {
setVerifyButton(false);
setModalMessage("Please enter your email");
modal1.current.open();
} else if (data.username != "" && data.password == "") {
setVerifyButton(false);
setModalMessage("Please enter your password");
modal1.current.open();
} else {
let loginData = await fetch(
"https://domain.com/api/v1/login",
{
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
email: data.username,
password: data.password,
}),
}
);
let jsonLoginData = await loginData.json();
if (jsonLoginData.status == "success") {
setVerifyButton(false);
setUser("Riyas");
AsyncStorage.setItem("token", jsonLoginData.token);
console.log('AddDevice Call Happens Here');
AddFirebaseDevice(await AsyncStorage.getItem('fcmToken'));
dispatch({ type: "SIGN_IN", token: jsonLoginData.token });
} else if (jsonLoginData.status == "verification_needed") {
setVerifyButton(true);
AsyncStorage.setItem("vToken", jsonLoginData.token);
setModalMessage(jsonLoginData.description);
modal1.current.open();
} else {
setVerifyButton(false);
setModalMessage(jsonLoginData.description);
modal1.current.open();
}
}
},
signOut: async () => {
setUser(null);
await AsyncStorage.getItem("fcmToken").then(async (value) => {
if(value){
console.log('Firebase Remove');
console.log(await AsyncStorage.getItem("token"));
let removeDevice = await fetch(
"https://domain.com/api/v1/firebase/remove",
{
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: "Bearer " + (await AsyncStorage.getItem("token")),
},
body: JSON.stringify({
fcm_token: value
}),
}
);
let jsonRemoveData = await removeDevice.json();
console.log('Remove Device:'+JSON.stringify(jsonRemoveData));
}
AsyncStorage.removeItem("token");
});
dispatch({ type: "SIGN_OUT" });
},
signUp: async (data) => {
let registerData = await fetch(
"https://domain.com/api/v1/register",
{
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
name: data.name,
email: data.email,
password: data.password,
c_password: data.c_password,
}),
}
);
setVerifyButton(false);
let jsonRegisterData = await registerData.json();
console.log(jsonRegisterData);
if (jsonRegisterData.status == "success") {
setUser("Riyas");
AsyncStorage.setItem("token", jsonRegisterData.token);
dispatch({ type: "SIGN_IN", token: jsonRegisterData.token });
}
},
forgotPassword: async (data) => {
setVerifyButton(false);
if (data.email == "") {
setModalMessage("Please enter your email address");
modal1.current.open();
} else {
let forgotPasswordData = await fetch(
"https://domain.com/api/v1/forgot-password",
{
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
email: data.email,
}),
}
);
let jsonForgotPasswordData = await forgotPasswordData.json();
console.log(jsonForgotPasswordData);
setVerifyButton(false);
setModalMessage(jsonForgotPasswordData.message);
if (jsonForgotPasswordData.status == "success") {
modal1.current.open();
} else {
modal1.current.open();
}
}
},
resendVerifyEmail: async () => {
console.log("Resent");
let resendVerifyEmailData = await fetch(
"https://domain.com/api/v1/resend-verify-email",
{
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: "Bearer " + (await AsyncStorage.getItem("vToken")),
},
}
);
let jsonResendVerifyEmailData = await resendVerifyEmailData.json();
setVerifyButton(false);
if (jsonResendVerifyEmailData.status == "success") {
AsyncStorage.removeItem("vToken");
modal1.current.close();
} else {
modal1.current.open();
}
},
}),
[]
);
<AuthContext.Provider value={authContext}>
</AuthContext.Provider>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment