-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
59 lines (50 loc) · 1.34 KB
/
Copy pathauth.js
File metadata and controls
59 lines (50 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const FUSIONAUTH_DOMAIN = "https://YOUR-FUSIONAUTH-DOMAIN";
const CLIENT_ID = "YOUR_CLIENT_ID";
const REDIRECT_URI = "http://localhost:8044";
//
// 1) LOGIN – redirect FusionAuth
//
export function login() {
const url =
`${FUSIONAUTH_DOMAIN}/oauth2/authorize?` +
`client_id=${CLIENT_ID}&` +
`redirect_uri=${encodeURIComponent(REDIRECT_URI)}&` +
`response_type=code&` +
`scope=openid offline_access`;
window.location.href = url;
}
//
// 2) LOGOUT – FusionAuth logout
//
export function logout() {
const url =
`${FUSIONAUTH_DOMAIN}/oauth2/logout?client_id=${CLIENT_ID}&` +
`post_logout_redirect_uri=${encodeURIComponent(REDIRECT_URI)}`;
window.location.href = url;
}
//
// 3) INIT
//
export async function handleRedirect() {
const params = new URLSearchParams(window.location.search);
const authCode = params.get("code");
if (authCode) {
console.log("FusionAuth redirect detected. User is logged in.");
//No token exchange, session cookie already in browser
window.history.replaceState({}, document.title, "/");
}
}
//
// 4) USER INFO
//
export async function getUserInfo() {
try {
const response = await fetch(`${FUSIONAUTH_DOMAIN}/oauth2/userinfo`, {
credentials: "include"
});
if (!response.ok) return null;
return await response.json();
} catch (e) {
return null;
}
}