-
Notifications
You must be signed in to change notification settings - Fork 764
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff622e3
commit 9161055
Showing
9 changed files
with
139 additions
and
136 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
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
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,43 @@ | ||
import { setToken } from "~/utils/token"; | ||
import { fetchLogin, fetchSignUp } from "~/api/auth"; | ||
import { useUserStore } from "~/store/user"; | ||
|
||
async function login({ phone, password }: { phone: string; password: string }) { | ||
const userStore = useUserStore(); | ||
|
||
const data = await fetchLogin({ | ||
phone, | ||
password, | ||
}); | ||
|
||
setToken(data.token); | ||
userStore.initUser(data.user); | ||
} | ||
|
||
async function signup({ | ||
phone, | ||
name, | ||
password, | ||
}: { | ||
phone: string; | ||
name: string; | ||
password: string; | ||
}) { | ||
const userStore = useUserStore(); | ||
|
||
const data = await fetchSignUp({ | ||
phone, | ||
name, | ||
password, | ||
}); | ||
|
||
setToken(data.token); | ||
userStore.initUser(data.user); | ||
} | ||
|
||
export function useAuth() { | ||
return { | ||
login, | ||
signup | ||
}; | ||
} |
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,98 +1,86 @@ | ||
<template> | ||
<div | ||
className="flex min-h-full flex-1 flex-col justify-center px-6 py-12 lg:px-8" | ||
> | ||
<div className="sm:mx-auto sm:w-full sm:max-w-sm"> | ||
<img className="mx-auto h-10 w-auto" src="/logo.png" alt="earthworm" /> | ||
<h2 | ||
className="mt-10 text-center text-2xl font-bold leading-9 tracking-tight text-gray-900" | ||
> | ||
Log in to your account | ||
</h2> | ||
</div> | ||
<div className="flex min-h-full flex-1 flex-col justify-center px-6 py-12 lg:px-8"> | ||
<div className="sm:mx-auto sm:w-full sm:max-w-sm"> | ||
<img className="mx-auto h-10 w-auto" src="/logo.png" alt="earthworm" /> | ||
<h2 className="mt-10 text-center text-2xl font-bold leading-9 tracking-tight text-gray-900"> | ||
Log in to your account | ||
</h2> | ||
</div> | ||
|
||
<div className="mt-10 sm:mx-auto sm:w-full sm:max-w-sm"> | ||
<n-form ref="formRef" :rules="rules" :model="model"> | ||
<n-form-item path="phone" label="Phone" required> | ||
<n-input v-model:value="model.phone" @keydown.enter.prevent /> | ||
</n-form-item> | ||
<n-form-item path="password" label="Password" required> | ||
<n-input | ||
v-model:value="model.password" | ||
type="password" | ||
@keydown.enter.prevent | ||
/> | ||
</n-form-item> | ||
<n-button type="primary" size="large" block @click="handleLogin"> | ||
Log in | ||
</n-button> | ||
</n-form> | ||
<div className="mt-10 sm:mx-auto sm:w-full sm:max-w-sm"> | ||
<n-form ref="formEl" :rules="rules" :model="model"> | ||
<n-form-item path="phone" label="Phone" required> | ||
<n-input v-model:value="model.phone" @keydown.enter.prevent /> | ||
</n-form-item> | ||
<n-form-item path="password" label="Password" required> | ||
<n-input v-model:value="model.password" type="password" @keydown.enter.prevent /> | ||
</n-form-item> | ||
<n-button type="primary" size="large" block @click="handleLogin"> | ||
Log in | ||
</n-button> | ||
</n-form> | ||
|
||
<p className="mt-10 text-center text-sm text-gray-500"> | ||
Not a account? | ||
<NuxtLink | ||
href="/auth/signup" | ||
className="font-semibold leading-6 text-indigo-600 hover:text-indigo-500" | ||
> | ||
Sign up | ||
</NuxtLink> | ||
</p> | ||
<p className="mt-10 text-center text-sm text-gray-500"> | ||
Not a account? | ||
<NuxtLink href="/auth/signup" className="font-semibold leading-6 text-indigo-600 hover:text-indigo-500"> | ||
Sign up | ||
</NuxtLink> | ||
</p> | ||
</div> | ||
</div> | ||
</div> | ||
</template> | ||
<script setup lang="ts"> | ||
import { type FormInst, type FormRules } from "naive-ui"; | ||
import { login } from "../../api/auth"; | ||
import { setToken } from "~/utils/token"; | ||
import { useUserStore } from "~/store/user"; | ||
const formRef = ref<FormInst | null>(null); | ||
import { useAuth } from "~/composables/auth"; | ||
import { delay } from '~/utils'; | ||
interface ModelType { | ||
phone: string | null; | ||
password: string | null; | ||
phone: string | null; | ||
password: string | null; | ||
} | ||
const { login } = useAuth(); | ||
const formEl = ref<FormInst | null>(null); | ||
const model = ref<ModelType>({ | ||
phone: null, | ||
password: null, | ||
phone: null, | ||
password: null, | ||
}); | ||
const rules: FormRules = { | ||
phone: [ | ||
{ required: true, message: "Please input your phone number" }, | ||
{ | ||
pattern: /^1[3456789]\d{9}$/, | ||
message: "Please input correct phone number", | ||
}, | ||
], | ||
password: [ | ||
{ required: true, message: "Please input your password" }, | ||
{ min: 6, message: "Password length must be greater than 6" }, | ||
], | ||
phone: [ | ||
{ required: true, message: "Please input your phone number" }, | ||
{ | ||
pattern: /^1[3456789]\d{9}$/, | ||
message: "Please input correct phone number", | ||
}, | ||
], | ||
password: [ | ||
{ required: true, message: "Please input your password" }, | ||
{ min: 6, message: "Password length must be greater than 6" }, | ||
], | ||
}; | ||
const message = useMessage(); | ||
const router = useRouter(); | ||
const route = useRoute(); | ||
const userStore = useUserStore() | ||
async function gotoPreviousPage() { | ||
await delay(500); | ||
router.replace(route.query.callback?.toString() ?? "/"); | ||
} | ||
const handleLogin = () => { | ||
formRef.value?.validate(async (errors) => { | ||
if (!errors) { | ||
const data = await login({ | ||
phone: model.value.phone ?? "", | ||
password: model.value.password ?? "", | ||
}); | ||
if (data) { | ||
setToken(data.token); | ||
userStore.loginUser(data.user) | ||
message.success("login success"); | ||
setTimeout(() => { | ||
router.replace(route.query.callback?.toString() ?? "/"); | ||
}, 500); | ||
} | ||
} | ||
}); | ||
formEl.value?.validate(async (errors) => { | ||
if (!errors) { | ||
await login({ | ||
phone: model.value.phone ?? "", | ||
password: model.value.password ?? "", | ||
}); | ||
message.success("login success"); | ||
await gotoPreviousPage() | ||
} | ||
}); | ||
}; | ||
</script> |
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
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
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
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,7 @@ | ||
export function delay(timeout: number = 1000) { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve(""); | ||
}, timeout); | ||
}); | ||
} |