generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
93 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,84 @@ | ||
import { renderGitHubLoginButton } from "./github-login-button"; | ||
|
||
let gitHubAccessToken: null | string = null; | ||
|
||
export function checkForGitHubAccessToken(): string | null { | ||
const accessToken = localStorage.getItem("provider_token"); | ||
const expiresAt = localStorage.getItem("expires_at"); | ||
const oauthToken = getLocalStoreOauth(); | ||
|
||
if (expiresAt && parseInt(expiresAt, 10) < Date.now() / 1000) { | ||
// expired | ||
localStorage.removeItem("provider_token"); | ||
localStorage.removeItem("expires_at"); | ||
const expiresAt = oauthToken?.expires_at; | ||
if (expiresAt) { | ||
if (expiresAt < Date.now() / 1000) { | ||
localStorage.removeItem("sb-wfzpewmlyiozupulbuur-auth-token"); | ||
} | ||
} | ||
|
||
const accessToken = oauthToken?.provider_token; | ||
if (accessToken) { | ||
gitHubAccessToken = accessToken; | ||
return accessToken; | ||
} else { | ||
renderGitHubLoginButton(); | ||
return null; | ||
} | ||
return null; | ||
} | ||
|
||
export function getLocalStoreOauth(): ExampleAuthToken | null { | ||
const oauthToken = localStorage.getItem("sb-wfzpewmlyiozupulbuur-auth-token"); | ||
if (!oauthToken) return null; | ||
return JSON.parse(oauthToken); | ||
} | ||
|
||
export function getExistingSessionToken() { | ||
return gitHubAccessToken; | ||
interface ExampleAuthToken { | ||
provider_token: string; | ||
access_token: string; | ||
expires_in: number; | ||
expires_at: number; | ||
refresh_token: string; | ||
token_type: string; | ||
user: { | ||
id: string; | ||
aud: string; | ||
role: string; | ||
email: string; | ||
email_confirmed_at: string; | ||
phone: string; | ||
confirmed_at: string; | ||
last_sign_in_at: string; | ||
app_metadata: { provider: string; providers: string[] }; | ||
user_metadata: { | ||
avatar_url: string; | ||
email: string; | ||
email_verified: boolean; | ||
full_name: string; | ||
iss: string; | ||
name: string; | ||
phone_verified: boolean; | ||
preferred_username: string; | ||
provider_id: string; | ||
sub: string; | ||
user_name: string; | ||
}; | ||
identities: [ | ||
{ | ||
id: string; | ||
user_id: string; | ||
identity_data: { | ||
avatar_url: string; | ||
email: string; | ||
email_verified: boolean; | ||
full_name: string; | ||
iss: string; | ||
name: string; | ||
phone_verified: boolean; | ||
preferred_username: string; | ||
provider_id: string; | ||
sub: string; | ||
user_name: string; | ||
}; | ||
provider: string; | ||
last_sign_in_at: string; | ||
created_at: string; | ||
updated_at: string; | ||
}, | ||
]; | ||
created_at: string; | ||
updated_at: string; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters