Skip to content

Commit

Permalink
Merge pull request #4 from rohit-zip/main
Browse files Browse the repository at this point in the history
feature/google-login
  • Loading branch information
rohit-zip authored Jun 14, 2024
2 parents 62552d8 + 408fb1d commit 5f1fa49
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
1 change: 0 additions & 1 deletion app/(root)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export default function AuthLayout({
<GoogleOAuthProvider clientId={"50987300482-vlm1c14cr19nush8ib2hhv5deoi4ge08.apps.googleusercontent.com"}>
<RefreshTokenProvider>
<BloggiosToast/>
<RedirectProfileNotAdded/>
{children}
</RefreshTokenProvider>
</GoogleOAuthProvider>
Expand Down
4 changes: 2 additions & 2 deletions components/custom/forms/ProfileInitialForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {dispatchError} from "@/utils/DispatchFunctions";
import {useMutation} from "@tanstack/react-query";
import {redirect, useRouter} from "next/navigation";
import AuthenticatedAxiosInterceptor from "@/utils/AuthenticatedAxiosInterceptor";
import {BLOG_PAGE} from "@/constants/UiPathConstants";

export default function ProfileInitialForm() {

Expand All @@ -32,7 +33,6 @@ export default function ProfileInitialForm() {
const {isAuthenticated, authorities} = useSelector((state: RootState) => state.auth);
const [options, setOptions] = useState<string[]>([]);
const dispatch = useDispatch();
const router = useRouter();
const authAxios = AuthenticatedAxiosInterceptor();

const addProfileMutation = useMutation({
Expand All @@ -47,7 +47,7 @@ export default function ProfileInitialForm() {

useLayoutEffect(()=> {
if (isAuthenticated && !authorities?.includes('ROLE_DUMMY')) {
redirect("/dashboard");
redirect("/");
} else if (!isAuthenticated) {
redirect("/login");
}
Expand Down
2 changes: 1 addition & 1 deletion components/functional/RedirectProfileNotAdded.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const RedirectProfileNotAdded: React.FC = () => {

const {isAuthenticated, authorities} = useSelector((state: RootState) => state.auth);

useEffect(()=> {
useLayoutEffect(()=> {
if (isAuthenticated && authorities?.includes('ROLE_DUMMY')) {
redirect("/profile-addition-initial");
}
Expand Down
1 change: 1 addition & 0 deletions constants/ServiceConstants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export const EMAIL_REGEX: RegExp = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{
export const UUID_REGEX: RegExp = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
export const ACCOUNT_INACTIVE_ERROR_CODE: string = "DE__AUTH-2004";
export const chipColors = ['bg-purple-700', 'bg-red-700', 'bg-amber-700', 'bg-blue-700'];
export const PROFILE_NOT_FOUND_CODE = "DE__USER-2016";
8 changes: 6 additions & 2 deletions providers/RefreshTokenProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ import * as React from "react";
import {useLayoutEffect, useState} from "react";
import LoaderPage from "@/components/custom/loaders/LoaderPage";
import {refreshToken} from "@/rest/AuthProviderApplication";
import {useDispatch} from "react-redux";
import {useDispatch, useSelector} from "react-redux";
import {clearCredentials, setCredentials} from "@/state/authSlice";
// @ts-ignore
import Cookies from 'js-cookie';
import {addUserProfile} from "@/service/UserProviderApplication";
import {RootState} from "@/state/store";
import {redirect, useRouter} from "next/navigation";

export function RefreshTokenProvider({children}: Readonly<{ children: React.ReactNode }>) {

const [isChecking, setIsChecking] = useState<boolean>(true);
const {authorities} = useSelector((state: RootState) => state.auth);
const dispatch = useDispatch();
const router = useRouter();

useLayoutEffect(() => {
let isMounted = true;
Expand All @@ -32,7 +36,7 @@ export function RefreshTokenProvider({children}: Readonly<{ children: React.Reac
Cookies.set(process.env.NEXT_PUBLIC_COOKIE_TOKEN_NAME, response.headers[`${process.env.NEXT_PUBLIC_COOKIE_HEADER_NAME}`], {
expires: 1,
});
addUserProfile(dispatch);
addUserProfile(dispatch, router, response.data.authorities);
setIsChecking(false);
}
})
Expand Down
22 changes: 15 additions & 7 deletions service/UserProviderApplication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ import {Dispatch} from "redux";
import {loggedInUserProfile} from "@/rest/UserAuthProviderApplication";
import {setProfile} from "@/state/profileSlice";
import {dispatchError} from "@/utils/DispatchFunctions";
import {useSelector} from "react-redux";
import {RootState} from "@/state/store";
import {AxiosError} from "axios";
import {ACCOUNT_INACTIVE_ERROR_CODE, PROFILE_NOT_FOUND_CODE} from "@/constants/ServiceConstants";

export const addUserProfile = (dispatch: Dispatch) => {
loggedInUserProfile()
.then(response => {
dispatch(setProfile(response));
}).catch(error => {
dispatchError(dispatch, error);
})
export const addUserProfile = (dispatch: Dispatch, router: any, authorities: any) => {
loggedInUserProfile()
.then(response => {
dispatch(setProfile(response));
}).catch((error: any) => {
if (error?.response?.data?.code === PROFILE_NOT_FOUND_CODE && authorities?.includes('ROLE_DUMMY')) {
router.push("/profile-addition-initial");
} else {
dispatchError(dispatch, error);
}
})
}

0 comments on commit 5f1fa49

Please sign in to comment.