Skip to content

Commit

Permalink
fix: implement withGuard method on browser client plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Nov 27, 2023
1 parent 6534a98 commit e41c421
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/auth/plugins/japa/browser_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,34 @@ export const authBrowserClient = (app: ApplicationService) => {

decoratorsCollection.register({
context(context) {
context.withGuard = function (guardName) {
return {
async loginAs(user) {
const client = auth.createAuthenticatorClient()
const guard = client.use(guardName) as GuardContract<unknown>
const requestData = await guard.authenticateAsClient(user)

if (requestData.headers) {
throw new RuntimeException(
`Cannot use "${guard.driverName}" guard with browser client`
)
}

if (requestData.cookies) {
debug('defining cookies with browser context %O', requestData.cookies)
Object.keys(requestData.cookies).forEach((cookie) => {
context.setCookie(cookie, requestData.cookies![cookie])
})
}

if (requestData.session) {
debug('defining session with browser context %O', requestData.session)
context.setSession(requestData.session)
}
},
}
}

context.loginAs = async function (user) {
const client = auth.createAuthenticatorClient()
const guard = client.use() as GuardContract<unknown>
Expand Down

0 comments on commit e41c421

Please sign in to comment.