Web Guard
Web Guard 使用会话 /cookie 来登录用户。在创建服务器呈现的应用程序,或具有在同一域/子域上运行的第一方客户端的 API 时,您必须使用 Web Guard。
注:
Web Guard 依赖于@adonisjs/session
包。 确保首先安装并配置 。
登录
你可以使用 auth.attempt
或 auth.login
方法登录用户。auth.attempt
方法从数据库中查找用户并验证他们的密码。
- 如果用户凭据正确,它将在内部调用
auth.login
方法并创建会话。 - 否则会引发 InvalidCredentialsException 。
import Route from '@ioc:Adonis/Core/Route'
Route.post('login', async ({ auth, request, response }) => {
const email = request.input('email')
const password = request.input('password')
try {
// highlight-start
await auth.use('web').attempt(email, password)
response.redirect('/dashboard')
// highlight-end
} catch {
return response.badRequest('Invalid credentials')
}
})
你可以手动处理异常并返回响应,或者让异常自行处理并使用content negotiation.
auth.login
如果 auth.attempt
查找策略不适合你的用例,那么你可以手动查找用户,验证他们的密码并调用 auth.login
方法为他们创建会话。
import User from 'App/Models/User'
import Route from '@ioc:Adonis/Core/Route'
import Hash from '@ioc:Adonis/Core/Hash'
Route.post('login', async ({ auth, request, response }) => {
const email = request.input('email')
const password = request.input('password')
// 手动查找用户
const user = await User
.query()
.where('email', email)
.where('tenant_id', getTenantIdFromSomewhere)
.whereNull('is_deleted')
.firstOrFail()
// 验证密码
if (!(await Hash.verify(user.password, password))) {
return response.badRequest('无效的凭据')
}
// 创建会话
await auth.use('web').login(user)
})
auth.loginViaId
与 auth.login
方法类似,loginViaId
方法使用用户 ID 为用户创建登录会话。
// 使用 id 登录用户
await auth.use('web').loginViaId(1)
使用记住登录选项
所有登录方法 attempt
、login
和 loginViaId
都接受一个布尔值作为最后一个参数来为登录用户创建一个记住登录的 cookie。
const rememberMe = true
await auth.use('web').attempt(email, password, rememberMe)
await auth.use('web').login(user, rememberMe)
await auth.use('web').loginViaId(1, rememberMe)
如果用户会话过期,记住的 cookie 将用于为用户创建另一个会话。记住登录令牌存储在 users
表本身中,目前只允许一个记住登录令牌。
验证后续请求
用户使用登录会话登录后,你可以使用 auth.authenticate
方法对后续请求进行身份验证。它将验证用户会话并在数据库中查找用户。
如果会话无效或数据库中不存在用户,则会引发 AuthenticationException。
否则,就可以使用 auth.user
属性访问登录用户。
import Route from '@ioc:Adonis/Core/Route'
Route.get('dashboard', async ({ auth }) => {
await auth.use('web').authenticate()
console.log(auth.use('web').user!)
})
在每条路由中手动调用此方法是不切实际的,因此可以使用存储在 ./app/Middleware/Auth.ts
文件中的 auth 中间件。
[了解更多关于 auth 中间件的信息 →](https://docs.adonisjs.com/guides/auth/middleware)
登出
你可以通过调用 auth.logout
方法注销用户。它将结束用户登录会话和记住的登录 cookie。users
中的记住登录标记也设置为 null。
import Route from '@ioc:Adonis/Core/Route'
Route.post('/logout', async ({ auth, response }) => {
await auth.use('web').logout()
response.redirect('/login')
})
其他方法/属性
以下是 web
guard 可用的方法/属性列表。
viaRemember
确认当前请求是否使用记住令牌进行身份验证。这将在以下情况下设置为true
:
- 你最初使用记住的令牌登录用户。
- 登录会话在当前请求期间已过期,并且记住登录 cookie 存在并且对登录用户有效。
auth.use('web').viaRemember
isLoggedOut
查找用户在当前请求期间是否已注销。在调用 auth.logout
方法后,该值将是 true
。
await auth.use('web').logout()
auth.use('web').isLoggedOut
isLoggedIn
查找用户是否已登录。在调用 auth.login
方法或通过 auth.authenticate
检查后,该值为 true
。
await auth.use('web').authenticate()
auth.use('web').isLoggedIn // true
await auth.use('web').attempt(email, password)
auth.use('web').isLoggedIn // true
isGuest
查找用户是否为访客(表示未登录)。该值始终与 isLoggedIn
标志相反。
isAuthenticated
查找当前请求是否通过了身份验证检查。此标志与 isLoggedIn
标志不同,并且在 auth.login
调用期间未设置为 true。
await auth.use('web').authenticate()
auth.use('web').isAuthenticated // true
await auth.use('web').attempt(email, password)
auth.use('web').isAuthenticated // false
authenticationAttempted
查找是否已尝试对当前请求进行身份验证。当你调用 auth.authenticate
方法时,该值设置为 true
auth.use('web').authenticationAttempted // false
await auth.use('web').authenticate()
auth.use('web').authenticationAttempted // true
provider
对 guard 使用的底层用户辅助器的引用。
verifyCredentials
一种验证用户凭据的方法。auth.attempt
方法在后台使用此方法。当凭据无效时会引发 InvalidCredentialsException 异常。
try {
await auth.use('web').verifyCredentials(email, password)
} catch (error) {
console.log(error)
}
check
该方法与 auth.authenticate
方法相同。但是,当请求未经过身份验证时,它不会引发任何异常。将其视为检查用户是否登录的可选尝试。
await auth.use('web').check()
if (auth.use('web').isLoggedIn) {
}
本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。