♻️ (sign in)
This commit is contained in:
@@ -5,13 +5,10 @@
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
import { GithubToken } from '@/modules/user/interfaces/GithubToken'
|
||||
import { GithubTokenError } from '@/modules/user/interfaces/GithubTokenError'
|
||||
import { useGitHubLogin } from '@/hooks/useGitHubLogin.hook'
|
||||
import { defineComponent, onBeforeMount, ref } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
|
||||
const AUTHENTICATION_SERVER = 'https://litenote.li212.fr'
|
||||
import { signIn } from '@/modules/user/service/signIn'
|
||||
|
||||
export default defineComponent({
|
||||
name: 'Authorize',
|
||||
@@ -25,11 +22,7 @@ export default defineComponent({
|
||||
|
||||
onBeforeMount(async () => {
|
||||
if (code) {
|
||||
const authenticationServerURL = new URL(AUTHENTICATION_SERVER)
|
||||
authenticationServerURL.searchParams.set('code', code.toString())
|
||||
|
||||
const response = await fetch(authenticationServerURL.toString())
|
||||
const body = (await response.json()) as GithubToken | GithubTokenError
|
||||
const body = await signIn(code.toString())
|
||||
|
||||
if ('error' in body) {
|
||||
hasError.value = true
|
||||
|
||||
@@ -1,72 +1,30 @@
|
||||
import { computed, ref } from 'vue'
|
||||
|
||||
import { DataType } from '@/data/DataType.enum'
|
||||
import { data } from '@/data/data'
|
||||
import { confirmMessage } from '@/utils/notif'
|
||||
import { GithubAccessToken } from '@/data/models/GithubAccessToken'
|
||||
import { Octokit } from '@octokit/rest'
|
||||
import { GithubToken } from '@/modules/user/interfaces/GithubToken'
|
||||
import { addSeconds } from 'date-fns'
|
||||
import { getAccessToken, saveAccessToken } from '@/modules/user/service/signIn'
|
||||
|
||||
const personalTokenId = 'token'
|
||||
const username = ref<string | null>(null)
|
||||
const accessToken = ref<string | null>(null)
|
||||
|
||||
let init = true
|
||||
|
||||
export const useGitHubLogin = () => {
|
||||
const getAccessToken = async () => {
|
||||
const response = await data.get<
|
||||
DataType.GithubAccessToken,
|
||||
GithubAccessToken
|
||||
>(data.generateId(DataType.GithubAccessToken, personalTokenId))
|
||||
const saveAccessTokenToLocal = async () => {
|
||||
const response = await getAccessToken()
|
||||
username.value = response?.username || ''
|
||||
accessToken.value = response?.token || ''
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
if (init) {
|
||||
init = false
|
||||
getAccessToken()
|
||||
saveAccessTokenToLocal()
|
||||
}
|
||||
|
||||
const saveCredentials = async (githubToken: GithubToken) => {
|
||||
const actualPAT = await getAccessToken()
|
||||
const accessToken = await saveAccessToken(githubToken)
|
||||
|
||||
const expirationDate = addSeconds(
|
||||
new Date(),
|
||||
githubToken.expires_in
|
||||
).toISOString()
|
||||
|
||||
const refreshTokenExpirationDate = addSeconds(
|
||||
new Date(),
|
||||
githubToken.refresh_token_expires_in
|
||||
).toISOString()
|
||||
|
||||
const accessToken: GithubAccessToken = {
|
||||
...actualPAT,
|
||||
_id: data.generateId(DataType.GithubAccessToken, personalTokenId),
|
||||
$type: DataType.GithubAccessToken,
|
||||
token: githubToken.access_token,
|
||||
expiresIn: githubToken.expires_in,
|
||||
expirationDate,
|
||||
refreshToken: githubToken.refresh_token,
|
||||
refreshTokenExpiresIn: githubToken.refresh_token_expires_in,
|
||||
refreshTokenExpirationDate,
|
||||
username: ''
|
||||
}
|
||||
|
||||
const octokit = new Octokit({
|
||||
auth: accessToken.token
|
||||
})
|
||||
|
||||
const user = await octokit.request('GET /user')
|
||||
accessToken.username = user.data.login
|
||||
username.value = accessToken.username
|
||||
|
||||
await data.add(accessToken)
|
||||
getAccessToken()
|
||||
await saveAccessTokenToLocal()
|
||||
confirmMessage(`${accessToken.username} is logged in!`)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,76 +1,9 @@
|
||||
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'
|
||||
|
||||
export const refreshToken = async () => {
|
||||
const accessToken = await data.get<
|
||||
DataType.GithubAccessToken,
|
||||
GithubAccessToken
|
||||
>(data.generateId(DataType.GithubAccessToken, personalTokenId))
|
||||
if (!accessToken) {
|
||||
return
|
||||
}
|
||||
|
||||
console.log(
|
||||
new Date(accessToken.expirationDate) >= new Date(),
|
||||
accessToken.expirationDate,
|
||||
new Date()
|
||||
)
|
||||
|
||||
if (new Date(accessToken.expirationDate) >= new Date()) {
|
||||
const response = await fetch(GITHUB_URL, {
|
||||
method: 'POST',
|
||||
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 expirationDate = addMilliseconds(
|
||||
new Date(),
|
||||
githubToken.expires_in
|
||||
).toISOString()
|
||||
|
||||
const refreshTokenExpirationDate = addMilliseconds(
|
||||
new Date(),
|
||||
githubToken.refresh_token_expires_in
|
||||
).toISOString()
|
||||
|
||||
const updatedAccessToken: GithubAccessToken = {
|
||||
...accessToken,
|
||||
token: githubToken.access_token,
|
||||
expiresIn: githubToken.expires_in,
|
||||
expirationDate,
|
||||
refreshToken: githubToken.refresh_token,
|
||||
refreshTokenExpiresIn: githubToken.refresh_token_expires_in,
|
||||
refreshTokenExpirationDate
|
||||
}
|
||||
|
||||
await data.add<DataType.GithubAccessToken>({
|
||||
...updatedAccessToken
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const getFiles = async (
|
||||
owner: string,
|
||||
|
||||
110
src/modules/user/service/signIn.ts
Normal file
110
src/modules/user/service/signIn.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import { data } from '@/data/data'
|
||||
import { DataType } from '@/data/DataType.enum'
|
||||
import { GithubAccessToken } from '@/data/models/GithubAccessToken'
|
||||
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 AUTHENTICATION_SERVER = 'https://litenote.li212.fr'
|
||||
const personalTokenId = 'token'
|
||||
|
||||
export const signIn = async (
|
||||
code: string
|
||||
): Promise<GithubToken | GithubTokenError> => {
|
||||
const authenticationServerURL = new URL(AUTHENTICATION_SERVER)
|
||||
authenticationServerURL.searchParams.set('code', code)
|
||||
|
||||
const response = await fetch(authenticationServerURL.toString())
|
||||
const body = (await response.json()) as GithubToken | GithubTokenError
|
||||
|
||||
return body
|
||||
}
|
||||
|
||||
export const refreshToken = async () => {
|
||||
const accessToken = await data.get<
|
||||
DataType.GithubAccessToken,
|
||||
GithubAccessToken
|
||||
>(data.generateId(DataType.GithubAccessToken, personalTokenId))
|
||||
|
||||
if (!accessToken) {
|
||||
return null
|
||||
}
|
||||
|
||||
console.log(
|
||||
new Date(accessToken.expirationDate) >= new Date(),
|
||||
accessToken.expirationDate,
|
||||
new Date()
|
||||
)
|
||||
|
||||
if (new Date(accessToken.expirationDate) >= new Date()) {
|
||||
const authenticationServerURL = new URL(AUTHENTICATION_SERVER)
|
||||
authenticationServerURL.searchParams.set('type', 'refresh')
|
||||
|
||||
const response = await fetch(authenticationServerURL.toString(), {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
refresh_token: accessToken.refreshToken,
|
||||
grant_type: 'refresh_token'
|
||||
})
|
||||
})
|
||||
const githubToken = (await response.json()) as
|
||||
| GithubToken
|
||||
| GithubTokenError
|
||||
|
||||
if ('error' in githubToken) {
|
||||
return null
|
||||
}
|
||||
|
||||
return await saveAccessToken(githubToken)
|
||||
}
|
||||
|
||||
return accessToken
|
||||
}
|
||||
|
||||
export const getAccessToken = async () => {
|
||||
const response = await data.get<
|
||||
DataType.GithubAccessToken,
|
||||
GithubAccessToken
|
||||
>(data.generateId(DataType.GithubAccessToken, personalTokenId))
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
export const saveAccessToken = async (githubToken: GithubToken) => {
|
||||
const actualPAT = await getAccessToken()
|
||||
|
||||
const expirationDate = addMilliseconds(
|
||||
new Date(),
|
||||
githubToken.expires_in
|
||||
).toISOString()
|
||||
|
||||
const refreshTokenExpirationDate = addMilliseconds(
|
||||
new Date(),
|
||||
githubToken.refresh_token_expires_in
|
||||
).toISOString()
|
||||
|
||||
const accessToken: GithubAccessToken = {
|
||||
...actualPAT,
|
||||
_id: data.generateId(DataType.GithubAccessToken, personalTokenId),
|
||||
$type: DataType.GithubAccessToken,
|
||||
token: githubToken.access_token,
|
||||
expiresIn: githubToken.expires_in,
|
||||
expirationDate,
|
||||
refreshToken: githubToken.refresh_token,
|
||||
refreshTokenExpiresIn: githubToken.refresh_token_expires_in,
|
||||
refreshTokenExpirationDate,
|
||||
username: ''
|
||||
}
|
||||
|
||||
const octokit = new Octokit({
|
||||
auth: accessToken.token
|
||||
})
|
||||
|
||||
const user = await octokit.request('GET /user')
|
||||
accessToken.username = user.data.login
|
||||
|
||||
await data.add(accessToken)
|
||||
|
||||
return accessToken
|
||||
}
|
||||
Reference in New Issue
Block a user