Skip to content

Commit

Permalink
Merge pull request #50 from avantifellows/feat/dynamically-redirect-t…
Browse files Browse the repository at this point in the history
…o-user-group-on-logout

Feat: Dynamically redirect to user's auth-group on logout
  • Loading branch information
Bahugunajii authored Jul 29, 2024
2 parents 2f798e2 + 958df9e commit 70b0b0d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export interface AuthContextProps {
userId?: string | null;
userName?: string | null;
userDbId?: number | null;
group?: string | null;
logout: () => void;
}

export interface CurrentTimeProps {
Expand Down
4 changes: 2 additions & 2 deletions components/TopBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { MixpanelTracking } from "@/services/mixpanel";
import { MIXPANEL_EVENT } from "@/constants/config";

const TopBar = () => {
const { userName } = useAuth();
const { userName, logout } = useAuth();
const formatUserName = (userName: string) => {
const names = userName.split(' ');

Expand Down Expand Up @@ -45,7 +45,7 @@ const TopBar = () => {
deleteCookie("refresh_token", { path: '/', domain: '.avantifellows.org' });
localStorage.removeItem("access_token");
localStorage.removeItem("refresh_token");
window.location.reload();
logout();
MixpanelTracking.getInstance().trackEvent(MIXPANEL_EVENT.LOGOUT);
};

Expand Down
11 changes: 10 additions & 1 deletion services/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
const [loggedIn, setLoggedIn] = useState(false);
const [userId, setUserId] = useState<string | null>(null);
const [user, setUser] = useState<User | null>(null);
const [group, setGroup] = useState<string | null>(null);

useEffect(() => {
async function checkToken() {
Expand All @@ -30,6 +31,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
if (result.isValid) {
setLoggedIn(true);
setUserId(result.data.id);
setGroup(result.data.data.group);
const userData = await getUserName(result.data.id);
setUser(userData);
} else {
Expand All @@ -50,8 +52,15 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
const userName = `${user?.first_name || ''} ${user?.last_name || ''}`.trim();
const userDbId = user ? user.id : null;

const logout = () => {
setLoggedIn(false);
setUserId(null);
setUser(null);
router.push(`${api.portal.frontend.baseUrl}/?group=${group}&platform=gurukul`);
};

return (
<AuthContext.Provider value={{ loggedIn, userId, userName, userDbId }}>
<AuthContext.Provider value={{ loggedIn, userId, userName, userDbId, group, logout }}>
{children}
</AuthContext.Provider>
);
Expand Down

0 comments on commit 70b0b0d

Please sign in to comment.