generated from SoftwareBrothers/adminjs-feature-template
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for auth providers
- Loading branch information
Showing
5 changed files
with
141 additions
and
10 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,61 @@ | ||
import AdminJS, { CurrentAdmin } from "adminjs"; | ||
import { FastifyInstance } from "fastify"; | ||
import { AuthenticationOptions } from "../types.js"; | ||
import { WrongArgumentError } from "../errors.js"; | ||
|
||
const getRefreshTokenPath = (admin: AdminJS) => { | ||
const { refreshTokenPath, rootPath } = admin.options; | ||
const normalizedRefreshTokenPath = refreshTokenPath.replace(rootPath, ""); | ||
|
||
return normalizedRefreshTokenPath.startsWith("/") | ||
? normalizedRefreshTokenPath | ||
: `/${normalizedRefreshTokenPath}`; | ||
}; | ||
|
||
const MISSING_PROVIDER_ERROR = | ||
'"provider" has to be configured to use refresh token mechanism'; | ||
|
||
export const withRefresh = ( | ||
fastifyApp: FastifyInstance, | ||
admin: AdminJS, | ||
auth: AuthenticationOptions | ||
): void => { | ||
const refreshTokenPath = getRefreshTokenPath(admin); | ||
|
||
const { provider } = auth; | ||
|
||
fastifyApp.post(refreshTokenPath, async (request, reply) => { | ||
if (!provider) { | ||
throw new WrongArgumentError(MISSING_PROVIDER_ERROR); | ||
} | ||
|
||
const updatedAuthInfo = await provider.handleRefreshToken( | ||
{ | ||
data: request.body ?? {}, | ||
query: request.query ?? {}, | ||
params: request.params ?? {}, | ||
headers: request.headers, | ||
}, | ||
{ request, reply } | ||
); | ||
|
||
let admin = request.session.adminUser as Partial<CurrentAdmin> | null; | ||
if (!admin) { | ||
admin = {}; | ||
} | ||
|
||
if (!admin._auth) { | ||
admin._auth = {}; | ||
} | ||
|
||
admin._auth = { | ||
...admin._auth, | ||
...updatedAuthInfo, | ||
}; | ||
|
||
request.session.set('adminUser', admin); | ||
request.session.save(() => { | ||
reply.send(admin); | ||
}); | ||
}); | ||
}; |
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 |
---|---|---|
@@ -1,5 +1,20 @@ | ||
import { BaseAuthProvider } from "adminjs"; | ||
import { FastifyReply, FastifyRequest } from "fastify"; | ||
|
||
export type AuthenticationOptions = { | ||
cookiePassword: string; | ||
cookieName?: string; | ||
authenticate: (email: string, password: string) => unknown | null; | ||
authenticate?: (email: string, password: string, context?: AuthenticationContext) => unknown | null; | ||
provider?: BaseAuthProvider; | ||
}; | ||
|
||
export type AuthenticationContext = { | ||
/** | ||
* @description Authentication request object | ||
*/ | ||
request: FastifyRequest; | ||
/** | ||
* @description Authentication response object | ||
*/ | ||
reply: FastifyReply; | ||
}; |