A boilerplate for creating custom AdonisJS Ally drivers
This repo is a starting point to create your custom OAuth2 drivers for AdonisJS ally.
The boilerplate is tailored to create one driver per project and publish it as a package on npm.
Following are the steps to get started.
-
Fork this repo and then clone it on your local machine.
-
Install all the dependencies using
npm,pnpm, oryarn(whatever you prefer). -
Open the
package.jsonfile and update the packagenameanddescription.{ "name": "", "description": "", }
The code for the driver is inside the src directory. Make sure to change the YourDriver keyword references inside the src/driver.ts file with the service name for which you are creating the driver. For example, Change YourDriver to AppleDriver or InstagramDriver.
The driver implementation is mainly driven by the config, except for the user and the userFromToken methods. Both of these methods are specific to the Oauth provider, so you have to implement them yourself.
The src/driver.ts file has the following exports.
The type defines the properties on the access token returned by the driver. You must read your OAuth provider documentation and list all the properties here.
Do not change the pre-defined token and bearer properties.
export type YourDriverAccessToken = {
token: string
type: 'bearer'
}Define a union of driver scopes accepted by your OAuth provider. You can check out the official implementations to see how they are defined.
The type defines the configuration options that your driver expects. It must specify the following properties and any additional properties your driver needs to be functional.
export type YourDriverConfig = {
driver: 'YourDriverName'
clientId: string
clientSecret: string
callbackUrl: string
authorizeUrl?: string
accessTokenUrl?: string
userInfoUrl?: string
}The driver implementation is a standard TypeScript class that extends the base Oauth2Driver class. The base driver class forces you to define the following instance properties.
authorizeUrlis the URL for the redirect request. The user is redirected to this URL to authorize the request. Check out provider docs to find this URL.accessTokenUrlis used to exchange the authorization code for the access token. Check out provider docs to find this URL.userInfoUrlis used to get the user profile information.codeParamNameis the query string parameter for reading the authorization code after redirecting the user back to the callback URL.errorParamNameis the query string parameter for finding the error after redirecting the user to the callback URL.stateCookieNameis the cookie name for storing the CSRF token (also known as the state). Make sure the cookie name does not collide with other drivers. A safer option is to prefix the driver name followed by theoauth_statekeyword.stateParamNameis the query string parameter name for setting the state during the authorization redirect.scopeParamNameis the query string parameter name for sending the scopes during the authorization redirect.scopesSeparatoris the character used for separating multiple parameters.
A factory function to reference the driver within the config/ally.ts file of an AdonisJS application. For example:
import { YourDriverService } from 'your-package-name'
defineConfig({
github: YourDriverService({
clientId: env.get('GITHUB_CLIENT_ID')!,
clientSecret: env.get('GITHUB_CLIENT_SECRET')!,
callbackUrl: '',
}),
})- I have renamed all
YourDriverreferences to a more meaningful name inside thesrc/driver.tsfile. - I have defined the
authorizeUrlclass property. - I have defined the
accessTokenUrlclass property. - I have defined the
userInfoUrlclass property. - I have defined the
codeParamNameclass property. - I have defined the
errorParamNameclass property. - I have defined the
stateCookieNameclass property. - I have defined the
stateParamNameclass property. - I have defined the
scopeParamNameclass property. - I have defined the
scopesSeparatorclass property. - I have implemented the
accessDeniedclass method. - I have implemented the
userclass method. - I have implemented the
userFromTokenclass method.
You can test the driver by installing it locally inside your AdonisJS application. Following are the steps you need to perform.
- Compile the TypeScript code to JavaScript using the
npm run buildscript. cdinto your AdonisJS project and install the package locally usingnpm i path/to/your/driver/package.- Finally, reference the driver using the
YourDriverServicefactory function inside theconfig/ally.tsfile.
You can configure the redirect request by implementing the configureRedirectRequest method on the driver class. The method is already pre-defined and commented out.
protected configureRedirectRequest(request: RedirectRequest<YourDriverScopes>) {
request.param('key', 'value')
}You can configure the access token request by implementing the configureAccessTokenRequest method on the driver class. The method is already pre-defined and commented out.
protected configureAccessTokenRequest(request: ApiRequest) {
// Request body
request.field('key', 'value')
// Query param
request.param('key', 'value')
}Are you excited about sharing your work with others? Submit your package to the awesome-adonisjs repo.