-
-
Notifications
You must be signed in to change notification settings - Fork 63
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
4690505
commit c61447f
Showing
3 changed files
with
160 additions
and
44 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 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,90 @@ | ||
/* | ||
* @adoniss/auth | ||
* | ||
* (c) AdonisJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/// <reference types="@japa/plugin-adonisjs" /> | ||
/// <reference types="@adonisjs/session/plugins/browser_client" /> | ||
|
||
import { RuntimeException } from '@poppinss/utils' | ||
import type { PluginFn } from '@japa/runner/types' | ||
import { decoratorsCollection } from '@japa/browser-client' | ||
import type { ApplicationService } from '@adonisjs/core/types' | ||
|
||
import debug from '../../debug.js' | ||
import type { Authenticators, GuardContract, GuardFactory } from '../../types.js' | ||
|
||
declare module 'playwright' { | ||
export interface BrowserContext { | ||
/** | ||
* Login a user using the default authentication | ||
* guard when using the browser context to | ||
* make page visits | ||
*/ | ||
loginAs(user: { | ||
[K in keyof Authenticators]: Authenticators[K] extends GuardFactory | ||
? ReturnType<Authenticators[K]> extends GuardContract<infer A> | ||
? A | ||
: never | ||
: never | ||
}): Promise<void> | ||
|
||
/** | ||
* Define the authentication guard for login | ||
*/ | ||
withGuard<K extends keyof Authenticators>( | ||
guard: K | ||
): { | ||
/** | ||
* Login a user using a specific auth guard | ||
*/ | ||
loginAs( | ||
user: Authenticators[K] extends GuardFactory | ||
? ReturnType<Authenticators[K]> extends GuardContract<infer A> | ||
? A | ||
: never | ||
: never | ||
): Promise<void> | ||
} | ||
} | ||
} | ||
|
||
export const authBrowserClient = (app: ApplicationService) => { | ||
const pluginFn: PluginFn = async function () { | ||
debug('installing auth browser client plugin') | ||
|
||
const auth = await app.container.make('auth.manager') | ||
|
||
decoratorsCollection.register({ | ||
context(context) { | ||
context.loginAs = async function (user) { | ||
const client = auth.createAuthenticatorClient() | ||
const guard = client.use() 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) | ||
} | ||
} | ||
}, | ||
}) | ||
} | ||
|
||
return pluginFn | ||
} |