Skip to content

Commit

Permalink
feat: basic authentication (#6)
Browse files Browse the repository at this point in the history
* chore: authentication api

* chore: basic authentication

* fix: lint and typecheck

* chore: lint

* fix: tsconfig
  • Loading branch information
YuJianghao authored May 28, 2022
1 parent 2ac80f7 commit c15050f
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 33 deletions.
16 changes: 16 additions & 0 deletions app/api/auth.ts
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'))
}
2 changes: 2 additions & 0 deletions app/composables/cookies.ts
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 })
10 changes: 9 additions & 1 deletion app/pages/index.vue
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>
27 changes: 27 additions & 0 deletions app/pages/signin.vue
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>
14 changes: 14 additions & 0 deletions app/router.ts
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
}
})
11 changes: 0 additions & 11 deletions app/tsconfig.json

This file was deleted.

4 changes: 4 additions & 0 deletions app/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const forceReloadWindow = () => {
window.onbeforeunload = () => {}
window.location.reload()
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@
},
"homepage": "https://github.com/YuJianghao/imageur#readme",
"dependencies": {
"@vueuse/core": "^8.5.0",
"@vueuse/integrations": "^8.5.0",
"@winwin/koa-authentication": "^0.2.2",
"axios": "^0.27.2",
"http-errors": "^2.0.0",
"koa": "^2.13.4",
"koa-bodyparser": "^4.3.0",
"koa-logger": "^3.2.1",
"universal-cookie": "^4.0.4",
"vue": "^3.2.36",
"vue-router": "^4.0.15"
},
Expand All @@ -51,6 +54,7 @@
"@types/koa-bodyparser": "^4.3.7",
"@types/koa-logger": "^3.1.2",
"@types/koa__router": "^8.0.11",
"@types/node": "^17.0.36",
"@types/rollup-plugin-auto-external": "^2.0.2",
"@vitejs/plugin-vue": "^2.3.3",
"concurrently": "^7.2.0",
Expand Down
Loading

0 comments on commit c15050f

Please sign in to comment.