Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#215 #216

Merged
merged 1 commit into from
Dec 4, 2023
Merged

#215 #216

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/hooks/useOneDriveAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import {
useState,
} from 'react'

const TOKEN_STORAGE_KEY =
'00000000-0000-0000-0078-f20226514725.9188040d-6c67-4c5b-b112-36a304b66dad-login.windows.net-accesstoken-6a5dfe6b-7b41-4f43-a4f3-5c6e434056e1-9188040d-6c67-4c5b-b112-36a304b66dad-user.read files.readwrite.all files.read.all openid profile--'
const TOKEN_STORAGE_KEY = 'oneDriveToken'

type Props = {
msalInstance: PublicClientApplication
Expand All @@ -22,7 +21,8 @@ type Props = {
}

const getStoredToken = (): MicrosoftToken | null => {
const storedTokenObject = sessionStorage.getItem(TOKEN_STORAGE_KEY)
let storedTokenObject = localStorage.getItem(TOKEN_STORAGE_KEY)

if (!storedTokenObject) return null

const storedToken = JSON.parse(storedTokenObject)
Expand Down Expand Up @@ -60,17 +60,29 @@ const useOneDriveAuth = ({
const handleSignIn = useCallback(async () => {
if (msalInstance) {
await msalInstance.initialize()
await signIn()
const storedToken = getStoredToken()
storedToken && setToken(storedToken)
await signIn().then(
async (response: AuthenticationResult | null) => {
if (response) {
const token: MicrosoftToken = {
secret: response.accessToken,
expiresOn: response.expiresOn!.getTime(),
}
setToken(token)
localStorage.setItem(
TOKEN_STORAGE_KEY,
JSON.stringify(token),
)
}
},
)
}
}, [msalInstance, signIn])

const handleSignOut = useCallback(() => {
setToken(null)
setUser(undefined)
setOneDriveFiles(undefined)
sessionStorage.removeItem(TOKEN_STORAGE_KEY)
localStorage.removeItem(TOKEN_STORAGE_KEY)
}, [setUser, setOneDriveFiles])

useEffect(() => {
Expand Down
Loading