Skip to content

Commit

Permalink
feat: added signin persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
Sayantan-s committed Jul 20, 2024
1 parent 9866a95 commit 9471fe0
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 57 deletions.
4 changes: 2 additions & 2 deletions api/db/models/user.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from peewee import CharField,TextField, DateTimeField
from peewee import CharField,TextField, DateTimeField, BooleanField
from . import BaseModel
from datetime import datetime
class User(BaseModel):
id = CharField(primary_key=True)
name = TextField(column_name='name')
email = CharField(column_name='email')
picture = TextField(column_name='profile_pic')
new_user = TextField(column_name='new_user')
new_user = BooleanField(column_name='new_user')
provider = CharField(column_name='provider')
created_at = DateTimeField(default=datetime.utcnow, column_name='created_at')
updated_at = DateTimeField(default=datetime.utcnow, column_name='updated_at')
Expand Down
39 changes: 20 additions & 19 deletions client/src/apis/hooks/auth/useAuthState.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import { User } from "firebase/auth";
import { isAxiosError } from "axios";
import { useEffect, useState } from "react";
import Firebase from "../../../integrations/firebase";

const loadUser = () =>
new Promise<User | null>((resolve, reject) =>
Firebase.auth.onAuthStateChanged((user) =>
user ? resolve(user) : reject()
)
);
import { useAuthStore } from "../../../store/auth";
import { getSigninMetaData } from "../../http/endpoints/auth";

export const useAuthState = () => {
const [isLoading, setIsLoading] = useState(false);
const { setLogin, setPendingStatus } = useAuthStore();

useEffect(() => {
setIsLoading(true);
const unsubscribe = Firebase.auth.onAuthStateChanged(
async (user) => {
setIsLoading(false);
console.log(user);
},
(error) => {
console.log(error);
},
() => {
console.log("compeleted...");
const unsubscribe = Firebase.auth.onAuthStateChanged(async (user) => {
setPendingStatus(true);
try {
if (user) {
const signInRes = await getSigninMetaData();
if (signInRes.status === 200) console.log("Logged in!!");
if (signInRes.status === 201) console.log("Signed up!!");
setLogin(signInRes.data.data);
}
} catch (error) {
if (isAxiosError(error)) {
await Firebase.auth.signOut();
}
} finally {
setPendingStatus(false);
}
);
});
return () => {
unsubscribe();
};
Expand Down
10 changes: 1 addition & 9 deletions client/src/apis/hooks/auth/useGoogleLogin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { useQueryClient } from "@tanstack/react-query";
import { isAxiosError } from "axios";
import { GoogleAuthProvider, signInWithPopup } from "firebase/auth";
import Firebase from "../../../integrations/firebase";
import { api } from "../../http";
import { SIGNIN } from "../../http/endpoints/auth";

export const GOOGLE_LOGIN = "GOOGLE_LOGIN_Q_KEY" as const;

Expand All @@ -16,13 +13,8 @@ export const useAuth = () => {
queryFn: async () => {
try {
await signInWithPopup(Firebase.auth, new GoogleAuthProvider());
const signInRes = await api.get(SIGNIN);
if (signInRes.status === 200) console.log("Logged in!!");
if (signInRes.status === 201) console.log("Signed up!!");
} catch (error) {
if (isAxiosError(error)) {
await Firebase.auth.signOut();
}
await Firebase.auth.signOut();
}
},
}),
Expand Down
8 changes: 7 additions & 1 deletion client/src/apis/http/endpoints/auth.ts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
export const SIGNIN = "/auth/signin";
import { api, IResponse } from "..";
import { IUser } from "../../../store/auth/types";

const SIGNIN = "/auth/signin";

export const getSigninMetaData = async () =>
await api.get<IResponse<IUser>>(SIGNIN);
6 changes: 6 additions & 0 deletions client/src/apis/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import axios from "axios";
import { getIdToken } from "firebase/auth";
import Firebase from "../../integrations/firebase";

export interface IResponse<TData> {
requestId: string;
status: number;
data: TData;
success: boolean;
}
export const api = axios.create({
baseURL: `${import.meta.env.PUBLIC_API_URI}/api/v1`,
headers: {
Expand Down
21 changes: 0 additions & 21 deletions client/src/store/auth.ts

This file was deleted.

10 changes: 5 additions & 5 deletions client/src/store/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { User } from "firebase/auth";
import { create } from "zustand";
import { IUser } from "./types";

interface State {
isAuthenticated: boolean;
user: User | null;
user: IUser | null;
isPending: boolean;
}

interface Action {
setLogin: (payload: User) => void;
setLogin: (payload: IUser) => void;
setPendingStatus: (pendingStatus: boolean) => void;
}

Expand All @@ -18,8 +18,8 @@ const useRootState = create<State & Action>((set) => ({
user: null,
setPendingStatus: (pendingStatus) =>
set(() => ({ isPending: pendingStatus })),
setLogin: (user: User) => set(() => ({ isAuthenticated: true, user })),
setLogin: (user: IUser) => set(() => ({ isAuthenticated: true, user })),
setLogout: () => set(() => ({ isAuthenticated: false, user: null })),
}));

export const useAuth = () => useRootState((state) => state);
export const useAuthStore = () => useRootState((state) => state);
10 changes: 10 additions & 0 deletions client/src/store/auth/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface IUser {
created_at: Date;
email: string;
id: string;
name: string;
new_user: boolean;
picture: string;
provider: string;
updated_at: Date;
}

0 comments on commit 9471fe0

Please sign in to comment.