♻️ (sign in)
This commit is contained in:
@@ -5,13 +5,10 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { GithubToken } from '@/modules/user/interfaces/GithubToken'
|
|
||||||
import { GithubTokenError } from '@/modules/user/interfaces/GithubTokenError'
|
|
||||||
import { useGitHubLogin } from '@/hooks/useGitHubLogin.hook'
|
import { useGitHubLogin } from '@/hooks/useGitHubLogin.hook'
|
||||||
import { defineComponent, onBeforeMount, ref } from 'vue'
|
import { defineComponent, onBeforeMount, ref } from 'vue'
|
||||||
import { useRoute, useRouter } from 'vue-router'
|
import { useRoute, useRouter } from 'vue-router'
|
||||||
|
import { signIn } from '@/modules/user/service/signIn'
|
||||||
const AUTHENTICATION_SERVER = 'https://litenote.li212.fr'
|
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
name: 'Authorize',
|
name: 'Authorize',
|
||||||
@@ -25,11 +22,7 @@ export default defineComponent({
|
|||||||
|
|
||||||
onBeforeMount(async () => {
|
onBeforeMount(async () => {
|
||||||
if (code) {
|
if (code) {
|
||||||
const authenticationServerURL = new URL(AUTHENTICATION_SERVER)
|
const body = await signIn(code.toString())
|
||||||
authenticationServerURL.searchParams.set('code', code.toString())
|
|
||||||
|
|
||||||
const response = await fetch(authenticationServerURL.toString())
|
|
||||||
const body = (await response.json()) as GithubToken | GithubTokenError
|
|
||||||
|
|
||||||
if ('error' in body) {
|
if ('error' in body) {
|
||||||
hasError.value = true
|
hasError.value = true
|
||||||
|
|||||||
@@ -1,72 +1,30 @@
|
|||||||
import { computed, ref } from 'vue'
|
import { computed, ref } from 'vue'
|
||||||
|
|
||||||
import { DataType } from '@/data/DataType.enum'
|
|
||||||
import { data } from '@/data/data'
|
|
||||||
import { confirmMessage } from '@/utils/notif'
|
import { confirmMessage } from '@/utils/notif'
|
||||||
import { GithubAccessToken } from '@/data/models/GithubAccessToken'
|
|
||||||
import { Octokit } from '@octokit/rest'
|
|
||||||
import { GithubToken } from '@/modules/user/interfaces/GithubToken'
|
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 username = ref<string | null>(null)
|
||||||
const accessToken = ref<string | null>(null)
|
const accessToken = ref<string | null>(null)
|
||||||
|
|
||||||
let init = true
|
let init = true
|
||||||
|
|
||||||
export const useGitHubLogin = () => {
|
export const useGitHubLogin = () => {
|
||||||
const getAccessToken = async () => {
|
const saveAccessTokenToLocal = async () => {
|
||||||
const response = await data.get<
|
const response = await getAccessToken()
|
||||||
DataType.GithubAccessToken,
|
|
||||||
GithubAccessToken
|
|
||||||
>(data.generateId(DataType.GithubAccessToken, personalTokenId))
|
|
||||||
username.value = response?.username || ''
|
username.value = response?.username || ''
|
||||||
accessToken.value = response?.token || ''
|
accessToken.value = response?.token || ''
|
||||||
|
|
||||||
return response
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (init) {
|
if (init) {
|
||||||
init = false
|
init = false
|
||||||
getAccessToken()
|
saveAccessTokenToLocal()
|
||||||
}
|
}
|
||||||
|
|
||||||
const saveCredentials = async (githubToken: GithubToken) => {
|
const saveCredentials = async (githubToken: GithubToken) => {
|
||||||
const actualPAT = await getAccessToken()
|
const accessToken = await saveAccessToken(githubToken)
|
||||||
|
|
||||||
const expirationDate = addSeconds(
|
await saveAccessTokenToLocal()
|
||||||
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()
|
|
||||||
confirmMessage(`${accessToken.username} is logged in!`)
|
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 { useGitHubLogin } from '@/hooks/useGitHubLogin.hook'
|
||||||
import { useMarkdown } from '@/hooks/useMarkdown.hook'
|
import { useMarkdown } from '@/hooks/useMarkdown.hook'
|
||||||
import { useNoteCache } from '@/modules/note/hooks/useNoteCache'
|
import { useNoteCache } from '@/modules/note/hooks/useNoteCache'
|
||||||
import { RepoFile } from '@/modules/repo/interfaces/RepoFile'
|
import { RepoFile } from '@/modules/repo/interfaces/RepoFile'
|
||||||
import { UserSettings } from '@/modules/repo/interfaces/UserSettings'
|
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 { 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 (
|
export const getFiles = async (
|
||||||
owner: string,
|
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