mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-03-04 02:50:59 +08:00
feat: login logic
This commit is contained in:
@@ -12,8 +12,9 @@ export default {
|
||||
const code = url.searchParams.get('code')
|
||||
const state = url.searchParams.get('state')
|
||||
const result = await githubExchange(code, state, '')
|
||||
if (result === 'NEED_REASON') {
|
||||
this.$router.push('/signup-reason?github=1')
|
||||
|
||||
if (result.needReason) {
|
||||
this.$router.push('/signup-reason?token=' + result.token)
|
||||
} else {
|
||||
this.$router.push('/')
|
||||
}
|
||||
|
||||
@@ -75,7 +75,6 @@ export default {
|
||||
toast.success('登录成功')
|
||||
this.$router.push('/')
|
||||
} else if (data.reason_code === 'NOT_VERIFIED') {
|
||||
sessionStorage.setItem('signup_username', data.username)
|
||||
toast.info('当前邮箱未验证,已经为您重新发送验证码')
|
||||
this.$router.push({ path: '/signup', query: { verify: 1, u: this.username } })
|
||||
} else if (data.reason_code === 'NOT_APPROVED') {
|
||||
@@ -89,11 +88,14 @@ export default {
|
||||
}
|
||||
},
|
||||
loginWithGoogle() {
|
||||
googleSignIn(() => {
|
||||
this.$router.push('/')
|
||||
}, () => {
|
||||
this.$router.push('/signup-reason?google=1')
|
||||
})
|
||||
googleSignIn(
|
||||
() => {
|
||||
this.$router.push('/')
|
||||
},
|
||||
(token) => {
|
||||
this.$router.push('/signup-reason?token=' + token)
|
||||
}
|
||||
)
|
||||
},
|
||||
loginWithGithub() {
|
||||
githubAuthorize()
|
||||
|
||||
@@ -86,7 +86,7 @@
|
||||
|
||||
<script>
|
||||
import { API_BASE_URL, toast } from '../main'
|
||||
import { googleSignIn, googleGetIdToken } from '../utils/google'
|
||||
import { googleSignIn } from '../utils/google'
|
||||
import { githubAuthorize } from '../utils/github'
|
||||
import BaseInput from '../components/BaseInput.vue'
|
||||
export default {
|
||||
@@ -145,15 +145,7 @@ export default {
|
||||
if (this.emailError || this.passwordError || this.usernameError) {
|
||||
return
|
||||
}
|
||||
if (this.registerMode === 'WHITELIST') {
|
||||
sessionStorage.setItem('signup_username', this.username)
|
||||
sessionStorage.setItem('signup_email', this.email)
|
||||
sessionStorage.setItem('signup_password', this.password)
|
||||
this.$router.push('/signup-reason')
|
||||
return
|
||||
}
|
||||
try {
|
||||
console.log('base url: ', API_BASE_URL)
|
||||
this.isWaitingForEmailSent = true
|
||||
const res = await fetch(`${API_BASE_URL}/api/auth/register`, {
|
||||
method: 'POST',
|
||||
@@ -194,8 +186,12 @@ export default {
|
||||
this.isWaitingForEmailVerified = false
|
||||
const data = await res.json()
|
||||
if (res.ok) {
|
||||
toast.success('注册成功,请登录')
|
||||
this.$router.push('/login')
|
||||
if (this.registerMode === 'WHITELIST') {
|
||||
this.$router.push('/signup-reason?token=' + data.token)
|
||||
} else {
|
||||
toast.success('注册成功,请登录')
|
||||
this.$router.push('/login')
|
||||
}
|
||||
} else {
|
||||
toast.error(data.error || '注册失败')
|
||||
}
|
||||
@@ -204,18 +200,11 @@ export default {
|
||||
}
|
||||
},
|
||||
signupWithGoogle() {
|
||||
if (this.registerMode === 'WHITELIST') {
|
||||
googleGetIdToken().then(token => {
|
||||
sessionStorage.setItem('google_id_token', token)
|
||||
this.$router.push('/signup-reason?google=1')
|
||||
}).catch(() => {})
|
||||
} else {
|
||||
googleSignIn(() => {
|
||||
googleSignIn(() => {
|
||||
this.$router.push('/')
|
||||
}, () => {
|
||||
this.$router.push('/signup-reason?google=1')
|
||||
}, (token) => {
|
||||
this.$router.push('/signup-reason?token=' + token)
|
||||
})
|
||||
}
|
||||
},
|
||||
signupWithGithub() {
|
||||
githubAuthorize()
|
||||
|
||||
@@ -19,8 +19,6 @@
|
||||
<script>
|
||||
import BaseInput from '../components/BaseInput.vue'
|
||||
import { API_BASE_URL, toast } from '../main'
|
||||
import { googleAuthWithToken } from '../utils/google'
|
||||
import { githubExchange } from '../utils/github'
|
||||
|
||||
export default {
|
||||
name: 'SignupReasonPageView',
|
||||
@@ -29,28 +27,14 @@ export default {
|
||||
return {
|
||||
reason: '',
|
||||
error: '',
|
||||
isGoogle: false,
|
||||
isGithub: false,
|
||||
isWaitingForRegister: false,
|
||||
googleToken: '',
|
||||
githubCode: ''
|
||||
token: '',
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
this.isGoogle = this.$route.query.google === '1'
|
||||
this.isGithub = this.$route.query.github === '1'
|
||||
if (this.isGoogle) {
|
||||
this.googleToken = sessionStorage.getItem('google_id_token') || ''
|
||||
if (!this.googleToken) {
|
||||
this.token = this.$route.query.token || ''
|
||||
if (!this.token) {
|
||||
this.$router.push('/signup')
|
||||
}
|
||||
} else if (this.isGithub) {
|
||||
this.githubCode = sessionStorage.getItem('github_code') || ''
|
||||
if (!this.githubCode) {
|
||||
this.$router.push('/signup')
|
||||
}
|
||||
} else if (!sessionStorage.getItem('signup_username')) {
|
||||
this.$router.push('/signup')
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
@@ -59,61 +43,33 @@ export default {
|
||||
this.error = '请至少输入20个字'
|
||||
return
|
||||
}
|
||||
if (this.isGoogle) {
|
||||
this.isWaitingForRegister = true
|
||||
const token = this.googleToken || sessionStorage.getItem('google_id_token')
|
||||
if (!token) {
|
||||
toast.error('Google 登录失败')
|
||||
return
|
||||
}
|
||||
await googleAuthWithToken(token, this.reason,
|
||||
() => { this.$router.push('/') },
|
||||
() => { this.error = 'Google 登录失败' }
|
||||
)
|
||||
this.isWaitingForRegister = false
|
||||
sessionStorage.removeItem('google_id_token')
|
||||
return
|
||||
}
|
||||
if (this.isGithub) {
|
||||
this.isWaitingForRegister = true
|
||||
const code = this.githubCode || sessionStorage.getItem('github_code')
|
||||
if (!code) {
|
||||
toast.error('GitHub 登录失败')
|
||||
return
|
||||
}
|
||||
const result = await githubExchange(code, '', this.reason)
|
||||
this.isWaitingForRegister = false
|
||||
sessionStorage.removeItem('github_code')
|
||||
if (result) {
|
||||
this.$router.push('/')
|
||||
} else {
|
||||
this.error = 'GitHub 登录失败'
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
this.isWaitingForRegister = true
|
||||
const res = await fetch(`${API_BASE_URL}/api/auth/register`, {
|
||||
const res = await fetch(`${API_BASE_URL}/api/auth/reason`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${this.token}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
username: sessionStorage.getItem('signup_username'),
|
||||
email: sessionStorage.getItem('signup_email'),
|
||||
password: sessionStorage.getItem('signup_password'),
|
||||
reason: this.reason
|
||||
})
|
||||
})
|
||||
this.isWaitingForRegister = false
|
||||
const data = await res.json()
|
||||
if (res.ok) {
|
||||
toast.success('验证码已发送,请查收邮箱')
|
||||
this.$router.push({ path: '/signup', query: { verify: 1, u: sessionStorage.getItem('signup_username') } })
|
||||
toast.success('注册理由已提交,请等待审核')
|
||||
this.$router.push('/')
|
||||
} else if (data.reason_code === 'INVALID_CREDENTIALS') {
|
||||
toast.error('登录已过期,请重新登录')
|
||||
this.$router.push('/login')
|
||||
} else {
|
||||
toast.error(data.error || '发送失败')
|
||||
toast.error(data.error || '提交失败')
|
||||
}
|
||||
} catch (e) {
|
||||
toast.error('发送失败')
|
||||
} finally {
|
||||
this.isWaitingForRegister = false
|
||||
toast.error('提交失败')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user