身份验证
登录
POST /api/login
后端默认开启了 CORS,如遇到跨域问题 及 HTTP STATUS 419 问题等等,请与后端沟通。 登录时,应 GET 请求 /api/csrf-cookie。如果不了解这是什么,请了解 CSRF 的理念。
async handleLogin () {
await axios.get(`/api/csrf-cookie`)
await axios.post(`/api/login`, {
email: "test@example.com",
password: "password",
remember: true
}).then(response => {
if (two_factor) {
// ...
} else {
// ...
}
console.log(response)
}).catch(error => {
console.error(error)
})
}
二次认证
POST /api/two-factor-challenge
如果用户开启了 二次认证,在登录时,需进行二次认证。
使用 “谷歌验证码” 验证
axios.post(`/api/two-factor-challenge`, {
code: ""
}).then(response => {
// successful && redirect to dashboard or back to specified redirect URL.
}).catch(error => {
console.error(error)
})
使用 “恢复代码” 验证
axios.post(`/api/two-factor-challenge`, {
recovery_code: ""
}).then(response => {
// successful && redirect to dashboard or back to specified redirect URL.
}).catch(error => {
console.error(error)
})
用户信息
GET /api/user
axios.get(`/api/user`).then(response => {
console.log(response)
}).catch(error => {
console.error(error)
})
注册
POST /api/register
# 前端 APP URL
VITE_APP_URL=https://www.example.com
假设新用户注册链接为:https://www.example.com/register?ref=100000 ,其中 ref 是推荐人的用户ID。
const ref = 0;
axios.post(`/api/register`, {
email: "new_user@example.com",
password: "password",
password_confirmation: "password",
name: "New User",
agreement: true,
ref: ref,
}).then(response => {
// successful
}).catch(error => {
console.error(error)
})
找回密码
POST /api/forgot-password
用户填写E-mail,发送重置密码链接至用户的E-mail。
axios.post(`/api/forgot-password`, {
email: "test@example.com"
}).then(response => {
// successful
}).catch(error => {
console.error(error)
})
重置密码
POST /api/reset-password
用户在邮箱点击重置密码链接并跳转至 “重置密码表单”。
后端默认的重置密码链接为:https://frontend-url.com/password-reset/:token?email=test@example.com
所以前端需要有 /password-reset/:token 这个路由,或者与后端协商更改为其他的路由。
import { useRoute } from 'vue-router'
const route = useRoute();
const token = route.params.token
axios.post(`/api/reset-password`, {
token: token,
email: "test@example.com",
password: "new_password",
password_confirmation: "new_password"
}).then(response => {
// successful
}).catch(error => {
console.error(error)
})
登出
POST /api/logout
axios.post(`/api/logout`, {}).then(response => {
console.log(response.status)
// successful && redirect to login page or home page
}).catch(error => {
console.error(error)
})
二次认证
这里的 “二次认证” 指的是 谷歌验证器 (Google Authenticator)。
获取 “验证登录密码” 状态
GET /api/user/confirmed-password-status
在开启二次认证等操作之前,应验证用户的登录密码,如果当前会话没有验证或已过期,需要通过 POST 请求 /api/user/confirm-password 验证用户的登录密码。
axios.get(`/api/user/confirmed-password-status`).then(response => {
if (confirmed) {
// next...
} else {
// show the confirm password form...
}
}).catch(error => {
console.error(error)
})
验证登录密码
POST /api/user/confirm-password
axios.post(`/api/user/confirm-password`, {
password: "password"
}).then(response => {
// next...
}).catch(error => {
console.error(error)
})
开启二次认证
POST /api/user/two-factor-authentication
开启二次认证需验证用户的登录密码。
axios.post(`/api/user/two-factor-authentication`).then(response => {
axios.get(`/api/user/two-factor-qr-code`)
axios.get(`/api/user/two-factor-secret-key`)
// ...
}).catch(error => {
console.error(error)
})
二次认证开启后,应通过 GET 请求 /api/user/two-factor-qr-code 以及 GET 请求 /api/user/two-factor-secret-key,获取 二维码 及 设置密钥。
确认开启二次认证
POST /api/user/confirmed-two-factor-authentication
用户扫描 二维码 或 设置密钥 后,用户需填写 谷歌验证器 (Google Authenticator) 上的6位数验证码。
axios.post(`/api/confirmed-two-factor-authentication`, {
code: ""
}).then(response => {
// show the recovery codes and "Regenerate recovery codes" button...
}).catch(error => {
console.error(error)
})
用户正确填写验证码后,则二次认证成功开启。此时,应向用户展示 “恢复代码” 及 “重新生成恢复代码” 按钮。
通过 GET 请求 /api/user/two-factor-recovery-codes 获取 “恢复代码”,通过 POST /api/user/two-factor-recovery-codes 重新生成恢复代码。
关闭二次认证
DELETE /api/user/two-factor-authentication
axios.delete(`/api/two-factor-authentication`, {}).then(() => {
// successful
}).catch(error => {
console.error(error)
})
修改登录密码
PUT /api/user/password
axios.get(`/api/user/password`, {
current_password: "current_password",
password: "new_password",
password_confirmation: "new_password"
}).then(() => {
// successful
}).catch(error => {
console.error(error)
})
修改个人资料
PUT /api/user/profile-information
axios.get(`/api/user/profile-information`, {
name: "New Name"
}).then(() => {
// successful
}).catch(error => {
console.error(error)
})
