Skip to content

Commit 77d5d7c

Browse files
committed
refactor: Improve login logic
1 parent 90fb7e2 commit 77d5d7c

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/constants/Endpoints.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const Endpoints = {
44
projectDiscordUrl: "https://discord.gg/6R4DK4G5Zh",
55
projectNpmUrl: "https://www.npmjs.com/package/react-chatbotify",
66
projectRepoUrl: "https://github.com/tjtanjin/react-chatbotify",
7+
galleryAuthRedirectUrl: `${import.meta.env.VITE_GALLERY_API_URL}/api/v1/auth/callback`,
78
fetchApiThemes: `${import.meta.env.VITE_GALLERY_API_URL}/api/v1/themes`,
89
fetchApiPlugins: `${import.meta.env.VITE_GALLERY_API_URL}/api/v1/plugins`,
910
fetchProjectDetails: `${import.meta.env.VITE_GALLERY_API_URL}/api/v1/projects`,

src/services/authService.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@ import { Endpoints } from "../constants/Endpoints";
77
*/
88
const handleLogin = (redirect_uri?: string) => {
99

10-
// if no uri specified to redirect to after login, then fallback to current location
11-
let final_redirect_uri = redirect_uri as string;
12-
if (!redirect_uri) {
13-
final_redirect_uri = window.location.href;
14-
}
10+
// if no uri specified to redirect to after login, then fallback to current location
11+
let final_redirect_uri = redirect_uri as string;
12+
if (!redirect_uri) {
13+
final_redirect_uri = window.location.href;
14+
}
1515

16-
// save redirect uri for later (post-login)
17-
localStorage.setItem('login_redirect_uri', final_redirect_uri);
16+
// save redirect uri for later (post-login)
17+
localStorage.setItem('login_redirect_uri', final_redirect_uri);
1818

19-
// oauth provider chosen (currently no parameter as github is the only one)
20-
const client_id = import.meta.env.VITE_GITHUB_APP_CLIENT_ID;
19+
// oauth provider chosen (currently no parameter as github is the only one)
20+
const client_id = import.meta.env.VITE_GITHUB_APP_CLIENT_ID;
2121

22-
// redirect user to login endpoint with client id
23-
window.location.href = `${Endpoints.gitHubLoginUrl}?client_id=${client_id}`;
22+
const redirectUri = `${Endpoints.galleryAuthRedirectUrl}?provider=github&redirect_url=${window.location.origin}`;
23+
const encodedRedirectUri = encodeURIComponent(redirectUri);
24+
// redirect user to login endpoint with client id
25+
window.location.href = `${Endpoints.gitHubLoginUrl}?client_id=${client_id}&redirect_uri=${encodedRedirectUri}`;
2426
}
2527

26-
export { handleLogin }
28+
export { handleLogin };
29+

0 commit comments

Comments
 (0)