Skip to content

Commit 0a99857

Browse files
author
sw-yx
committed
user
1 parent 60a9cd2 commit 0a99857

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/index.tsx

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React from 'react'
1+
import React from "react"
22

3-
import GoTrue, { User } from 'gotrue-js'
3+
import GoTrue, { User } from "gotrue-js"
44

55
type authChangeParam = (user?: User) => string | void
66

@@ -12,6 +12,7 @@ interface NIProps {
1212
const NetlifyIdentity = ({ children, domain, onAuthChange }: NIProps) =>
1313
children(useNetlifyIdentity(domain, onAuthChange))
1414

15+
export type User = User
1516
export default NetlifyIdentity
1617
export function useNetlifyIdentity(domain: string, onAuthChange: authChangeParam = () => {}) {
1718
const goTrueInstance = new GoTrue({
@@ -34,48 +35,48 @@ export function useNetlifyIdentity(domain: string, onAuthChange: authChangeParam
3435
const recoverAccount = (token: string, remember?: boolean | undefined) => goTrueInstance.recover(token, remember)
3536
const updateUser = (fields: Object) => {
3637
if (user == null) {
37-
throw new Error('No current user found - are you logged in?')
38+
throw new Error("No current user found - are you logged in?")
3839
} else {
3940
return user!
4041
.update(fields) // e.g. { email: "example@example.com", password: "password" }
4142
.then(_setUser)
4243
}
4344
}
4445
const getFreshJWT = () => {
45-
if (!user) throw new Error('No current user found - are you logged in?')
46+
if (!user) throw new Error("No current user found - are you logged in?")
4647
return user.jwt()
4748
}
4849
const logoutUser = () => {
49-
if (!user) throw new Error('No current user found - are you logged in?')
50+
if (!user) throw new Error("No current user found - are you logged in?")
5051
return user.logout().then(() => _setUser(undefined))
5152
}
5253

5354
const genericAuthedFetch = (method: string) => (endpoint: string, obj = {}) => {
54-
if (!user || !user.token || !user.token.access_token) throw new Error('no user token found')
55+
if (!user || !user.token || !user.token.access_token) throw new Error("no user token found")
5556
const defaultObj = {
5657
headers: {
57-
Accept: 'application/json',
58-
'Content-Type': 'application/json',
59-
Authorization: 'Bearer ' + user.token.access_token
58+
Accept: "application/json",
59+
"Content-Type": "application/json",
60+
Authorization: "Bearer " + user.token.access_token
6061
}
6162
}
6263
const finalObj = Object.assign(defaultObj, { method }, obj)
6364
return fetch(endpoint, finalObj).then(res =>
64-
finalObj.headers['Content-Type'] === 'application/json' ? res.json() : res
65+
finalObj.headers["Content-Type"] === "application/json" ? res.json() : res
6566
)
6667
}
6768
const authedFetch = {
68-
get: genericAuthedFetch('GET'),
69-
post: genericAuthedFetch('POST'),
70-
put: genericAuthedFetch('PUT'),
71-
delete: genericAuthedFetch('DELETE')
69+
get: genericAuthedFetch("GET"),
70+
post: genericAuthedFetch("POST"),
71+
put: genericAuthedFetch("PUT"),
72+
delete: genericAuthedFetch("DELETE")
7273
}
7374

7475
// // confirmation
7576
// http://lea.verou.me/2011/05/get-your-hash-the-bulletproof-way/
7677
React.useEffect(() => {
7778
const hash = window.location.hash.substring(1)
78-
if (hash.slice(0, 19) === 'confirmation_token=') {
79+
if (hash.slice(0, 19) === "confirmation_token=") {
7980
// we are in a confirmation!
8081
const token = hash.slice(19)
8182
goTrueInstance

0 commit comments

Comments
 (0)