-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: authentication api * chore: basic authentication * fix: lint and typecheck * chore: lint * fix: tsconfig
- Loading branch information
1 parent
2ac80f7
commit c15050f
Showing
12 changed files
with
237 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { request } from './request' | ||
import { cookies } from '~/composables/cookies' | ||
import { forceReloadWindow } from '~/utils' | ||
|
||
export async function signIn(username: string, password: string) { | ||
return request.post('/auth/login', { username, password }) | ||
} | ||
|
||
export async function signOut() { | ||
cookies.remove('token') | ||
forceReloadWindow() | ||
} | ||
|
||
export function isSignedIn() { | ||
return Boolean(cookies.get('token')) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { useCookies } from '@vueuse/integrations/useCookies' | ||
export const cookies = useCookies(null, { autoUpdateDependencies: true }) |
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,3 +1,11 @@ | ||
<script setup lang="ts"> | ||
import { signOut } from '~/api/auth' | ||
</script> | ||
|
||
<template> | ||
<div>hi pages</div> | ||
<div> | ||
<button @click="signOut"> | ||
signout | ||
</button> | ||
</div> | ||
</template> |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<script setup lang="ts"> | ||
import { reactive } from 'vue' | ||
import { signIn } from '~/api/auth' | ||
import { forceReloadWindow } from '~/utils' | ||
const form = reactive({ | ||
username: '', | ||
password: '', | ||
}) | ||
const onSubmit = () => { | ||
signIn(form.username, form.password) | ||
.then(() => { | ||
forceReloadWindow() | ||
}).catch((err) => { | ||
// eslint-disable-next-line no-alert | ||
window.alert(err) | ||
}) | ||
} | ||
</script> | ||
|
||
<template> | ||
<form action="" @submit.prevent="onSubmit"> | ||
<input v-model="form.username" type="text"> | ||
<input v-model="form.password" type="password"> | ||
<button>SignIn</button> | ||
</form> | ||
</template> |
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,6 +1,20 @@ | ||
import { createRouter, createWebHashHistory } from 'vue-router' | ||
import { isSignedIn } from './api/auth' | ||
import routes from '~pages' | ||
export const router = createRouter({ | ||
history: createWebHashHistory(), | ||
routes, | ||
}) | ||
|
||
router.beforeEach((to) => { | ||
if (isSignedIn()) { | ||
if (to.path === '/signin') | ||
return '/' | ||
else return true | ||
} | ||
else { | ||
if (to.path !== '/signin') | ||
return '/signin' | ||
return true | ||
} | ||
}) |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const forceReloadWindow = () => { | ||
window.onbeforeunload = () => {} | ||
window.location.reload() | ||
} |
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
Oops, something went wrong.