-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathauth.ts
More file actions
39 lines (34 loc) · 1.11 KB
/
auth.ts
File metadata and controls
39 lines (34 loc) · 1.11 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
import { createClient } from '@feathersdev/auth'
/**
* This is your public application id
*/
export const appId = import.meta.env.VITE_CLOUD_APP_ID as string
/**
* The Feathers auth client instance. You can use it to get a token,
* retrieve the current user and to log in and out.
*/
export const auth = createClient({
appId,
onLoginRequired: async (error) => {
// Redirect to login page if a login is required
// You can also do other things here like show a modal before redirecting
window.location.href = await auth.getLoginUrl(error)
},
})
/**
* Make an authenticated request to a server using the standard fetch API.
* Will redirect to the login page instead if the user needs to log in.
*
* @param url The URL for the request
* @param options Additional request options.
* @returns The fetch response
*/
export async function authFetch(url: string, options?: RequestInit) {
const headers = new Headers(options?.headers)
// Set the authorization header with the Feathers Auth token
headers.set('Authorization', await auth.getHeader())
return fetch(url, {
...options,
headers,
})
}