forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseAuth.ts
More file actions
71 lines (64 loc) · 1.77 KB
/
Copy pathuseAuth.ts
File metadata and controls
71 lines (64 loc) · 1.77 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
60
61
62
63
64
65
66
67
68
69
70
71
"use client";
import { useAuth as useOidcAuth } from "react-oidc-context";
import { useEffect, useState } from "react";
import { WebStorageStateStore } from "oidc-client-ts";
import { createCognitoAuthConfig } from "@/lib/auth";
interface CognitoAuthConfig {
authority?: string;
client_id?: string;
redirect_uri?: string;
post_logout_redirect_uri?: string;
response_type?: string;
scope?: string;
automaticSilentRenew?: boolean;
userStore?: WebStorageStateStore;
}
export function useAuth() {
const auth = useOidcAuth();
const [authConfig, setAuthConfig] = useState<CognitoAuthConfig | null>(null);
useEffect(() => {
async function loadConfig() {
try {
const config = await createCognitoAuthConfig();
setAuthConfig(config);
} catch (error) {
console.error("Failed to load auth configuration for signOut:", error);
}
}
loadConfig();
}, []);
// If no AuthProvider context, return mock auth state (no authentication)
if (!auth) {
return {
isAuthenticated: true,
user: null,
signIn: () => {},
signOut: () => {},
isLoading: false,
error: null,
token: null,
};
}
return {
isAuthenticated: auth.isAuthenticated,
user: auth.user,
signIn: auth.signinRedirect,
signOut: () => {
const clientId =
authConfig?.client_id || import.meta.env.VITE_COGNITO_CLIENT_ID || "";
const logoutUri =
authConfig?.redirect_uri ||
import.meta.env.VITE_COGNITO_REDIRECT_URI ||
"http://localhost:3000";
auth.signoutRedirect({
extraQueryParams: {
client_id: clientId,
logout_uri: logoutUri,
},
});
},
isLoading: auth.isLoading,
error: auth.error,
token: auth.user?.id_token,
};
}