Skip to content

Commit

Permalink
Improve login/logout logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tiago-bacelar committed Nov 11, 2023
1 parent 2b71c0d commit 4635b10
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 36 deletions.
37 changes: 18 additions & 19 deletions context/Auth/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,21 @@ export function AuthProvider({ children }) {
.then(({ jwt }) => {
localStorage.setItem(TOKEN_KEY_NAME, jwt);
API.defaults.headers.common["Authorization"] = `Bearer ${jwt}`;
api.getCurrentUser().then((response) => {
setUser(response);
switch (response.type) {
case USER.ROLES.ATTENDEE:
router.push("/attendee/profile");
break;
case USER.ROLES.SPONSOR:
router.push("/sponsor/scanner");
break;
case USER.ROLES.STAFF:
router.push("/staff/badges");
break;
default:
throw new Error(`Unknown USER TYPE: ${response.type}`);
api.getCurrentUser().then(async (response) => {
if (router.query.from == undefined) {
switch (response.type) {
case USER.ROLES.ATTENDEE:
await router.push({ query: { from: "/attendee/profile" } });
break;
case USER.ROLES.SPONSOR:
await router.push({ query: { from: "/sponsor/scanner" } });
break;
case USER.ROLES.STAFF:
await router.push({ query: { from: "/staff/badges" } });
break;
}
}
setUser(response);
});
})
.catch((errors) => {
Expand All @@ -148,11 +148,11 @@ export function AuthProvider({ children }) {
setErrors("Request denied by the server");
} else if (errors.request) {
setErrors(
"No connection to the server. Please check your internet connection and try again later",
"No connection to the server. Please check your internet connection and try again later"
);
} else {
setErrors(
"Something went wrong :/ Please check your internet connection and try again later",
"Something went wrong :/ Please check your internet connection and try again later"
);
}
setUser(undefined);
Expand All @@ -164,8 +164,7 @@ export function AuthProvider({ children }) {
setLoading(true);
localStorage.clear();
delete API.defaults.headers.common["Authorization"];
setUser(undefined);
router.push("/");
router.push("/").finally(() => setUser(undefined));
}

function editUser(nickname) {
Expand All @@ -179,7 +178,7 @@ export function AuthProvider({ children }) {
.catch((errors) => {
setUser(undefined);
setErrors(
"Something went wrong :/ Please check your internet connection and try again later",
"Something went wrong :/ Please check your internet connection and try again later"
);
});
}
Expand Down
11 changes: 7 additions & 4 deletions context/Auth/withAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function withAuth(WrappedComponent) {
const { user } = useAuth();

if (!user) {
router.replace("/signup");
router.replace(`/login?from=${encodeURIComponent(router.asPath)}`);
return null;
}

Expand All @@ -29,7 +29,8 @@ export function withAuth(WrappedComponent) {
"/product/[slug]",
].includes(router.pathname)
) {
return router.replace("/404");
router.replace("/404");
return null;
}
break;
case USER.ROLES.STAFF:
Expand All @@ -43,7 +44,8 @@ export function withAuth(WrappedComponent) {
"/attendees/[uuid]",
].includes(router.pathname)
) {
return router.replace("/404");
router.replace("/404");
return null;
}
break;
case USER.ROLES.SPONSOR:
Expand All @@ -55,7 +57,8 @@ export function withAuth(WrappedComponent) {
"/sponsor/visitors",
].includes(router.pathname)
) {
return router.replace("/404");
router.replace("/404");
return null;
}
break;
}
Expand Down
7 changes: 5 additions & 2 deletions context/Auth/withoutAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ export function withoutAuth(WrappedComponent) {
const router = useRouter();
const { user } = useAuth();

if (user && router.pathname == "/login") {
router.replace("/");
if (user) {
router.replace(
(router.query.from && decodeURIComponent(router.query.from)) ?? "/"
);
return null;
}
return <WrappedComponent {...props} />;
};
Expand Down
2 changes: 1 addition & 1 deletion layout/Challenges/Challenges.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ function Index() {
);
}

export default withoutAuth(Index);
export default Index;
2 changes: 1 addition & 1 deletion layout/FAQs/FAQs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ function Faq() {
);
}

export default withoutAuth(Faq);
export default Faq;
2 changes: 1 addition & 1 deletion layout/ForgotPassword/ForgotPassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ function ForgotPassword() {
);
}

export default withoutAuth(ForgotPassword);
export default ForgotPassword;
2 changes: 1 addition & 1 deletion layout/Hackathon/Hackathon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ function Index() {
);
}

export default withoutAuth(Index);
export default Index;
2 changes: 1 addition & 1 deletion layout/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ function Home() {
);
}

export default withoutAuth(Home);
export default Home;
2 changes: 1 addition & 1 deletion layout/ResetPassword/ResetPassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ function Reset() {
);
}

export default withoutAuth(Reset);
export default Reset;
2 changes: 1 addition & 1 deletion layout/Schedule/Schedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ function Index() {
);
}

export default withoutAuth(Index);
export default Index;
2 changes: 1 addition & 1 deletion layout/SignUp/SignUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ function Signup() {
);
}

export default withoutAuth(Signup);
export default Signup;
2 changes: 1 addition & 1 deletion layout/Speakers/Speakers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ function Speakers() {
);
}

export default withoutAuth(Speakers);
export default Speakers;
2 changes: 1 addition & 1 deletion layout/Team/Team.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ function Index() {
);
}

export default withoutAuth(Index);
export default Index;
2 changes: 1 addition & 1 deletion pages/register/[uuid].js
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,4 @@ function Register() {
);
}

export default withoutAuth(Register);
export default Register;

0 comments on commit 4635b10

Please sign in to comment.