Feat/GitHub auth (#6)
* ✨ (sign in) create sign in to github button * ✨ (github login) login via github button * 💄 (homepage) * ➕ (date fns) * ✨ (github login) refresh token when token expired
This commit is contained in:
@@ -1,9 +1,65 @@
|
||||
import { data } from '@/data/data'
|
||||
import { DataType } from '@/data/DataType.enum'
|
||||
import { GithubAccessToken } from '@/data/models/GithubAccessToken'
|
||||
import { useGitHubLogin } from '@/hooks/useGitHubLogin.hook'
|
||||
import { useMarkdown } from '@/hooks/useMarkdown.hook'
|
||||
import { useNoteCache } from '@/modules/note/hooks/useNoteCache'
|
||||
import { RepoFile } from '@/modules/repo/interfaces/RepoFile'
|
||||
import { UserSettings } from '@/modules/repo/interfaces/UserSettings'
|
||||
import { GithubToken } from '@/modules/user/interfaces/GithubToken'
|
||||
import { GithubTokenError } from '@/modules/user/interfaces/GithubTokenError'
|
||||
import { Octokit } from '@octokit/rest'
|
||||
import { addMilliseconds } from 'date-fns'
|
||||
|
||||
const personalTokenId = 'token'
|
||||
|
||||
const GITHUB_URL = 'https://github.com/login/oauth/access_token'
|
||||
|
||||
const refreshToken = async () => {
|
||||
const accessToken = await data.get<
|
||||
DataType.GithubAccessToken,
|
||||
GithubAccessToken
|
||||
>(data.generateId(DataType.GithubAccessToken, personalTokenId))
|
||||
if (!accessToken) {
|
||||
return
|
||||
}
|
||||
|
||||
if (new Date(accessToken.expirationDate) >= new Date()) {
|
||||
const response = await fetch(GITHUB_URL, {
|
||||
body: JSON.stringify({
|
||||
refresh_token: accessToken.refreshToken,
|
||||
grant_type: 'refresh_token'
|
||||
})
|
||||
})
|
||||
const githubToken = (await response.json()) as
|
||||
| GithubToken
|
||||
| GithubTokenError
|
||||
|
||||
if ('error' in githubToken) {
|
||||
return
|
||||
}
|
||||
|
||||
const updatedAccessToken: GithubAccessToken = {
|
||||
...accessToken,
|
||||
token: githubToken.access_token,
|
||||
expiresIn: githubToken.expires_in,
|
||||
expirationDate: addMilliseconds(
|
||||
new Date(),
|
||||
githubToken.expires_in
|
||||
).toISOString(),
|
||||
refreshToken: githubToken.refresh_token,
|
||||
refreshTokenExpiresIn: githubToken.refresh_token_expires_in,
|
||||
refreshTokenExpirationDate: addMilliseconds(
|
||||
new Date(),
|
||||
githubToken.refresh_token_expires_in
|
||||
).toISOString()
|
||||
}
|
||||
|
||||
await data.add<DataType.GithubAccessToken>({
|
||||
...updatedAccessToken
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const getFiles = async (
|
||||
owner: string,
|
||||
@@ -12,6 +68,7 @@ export const getFiles = async (
|
||||
if (!owner || !repo) {
|
||||
return []
|
||||
}
|
||||
await refreshToken()
|
||||
|
||||
const { accessToken } = useGitHubLogin()
|
||||
|
||||
@@ -47,6 +104,8 @@ export const getMainReadme = async (owner: string, repo: string) => {
|
||||
if (!owner || !repo) {
|
||||
return null
|
||||
}
|
||||
await refreshToken()
|
||||
|
||||
const { render } = useMarkdown()
|
||||
const { getCachedNote, saveCacheNote } = useNoteCache('README')
|
||||
|
||||
@@ -111,6 +170,8 @@ export const getFileContent = async (
|
||||
null
|
||||
}
|
||||
|
||||
await refreshToken()
|
||||
|
||||
const file = await octokit.request(
|
||||
'GET /repos/{owner}/{repo}/git/blobs/{file_sha}',
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user