Version 7.4 of adminjs package introduces authentication providers which both simplify and extend authentication possibilities in your application.
Every authentication provider extends BaseAuthProvider class (exported by adminjs package). adminjs also exports DefaultAuthProvider which functions exactly the same as the current authenticate method.
DefaultAuthProvider
As of version >=7.4.0 of adminjs, DefaultAuthProvider is an alternative to authenticate method. In the next major release, authenticate method will be removed in favour of auth providers.
import { DefaultAuthProvider } from'adminjs';import componentLoader from'<path to your component loader>';// Placeholder authentication function, add your logic for authenticating usersconstauthenticate= ({ email, password }, ctx) => {return { email };}constauthProvider=newDefaultAuthProvider({ componentLoader, authenticate,});// ...// Express example, in other plugins the change is exactly the same// "provider" should be configured at the same level as "authenticate" previouslyconstrouter=buildAuthenticatedRouter( admin, {// "authenticate" was here cookiePassword:'test', provider: authProvider, },null, { secret:'test', resave:false, saveUninitialized:true, } );
By migrating to class syntax, you should be able to modify any existing auth provider without making additional changes to your framework's plugin.
context of handleLogin will always be an object containing Request/Response objects specific to your framework of choice.
handleLogout and handleRefreshToken are optional. handleLogout will be called before your user's session is destroyed in case you have to perform additional actions to log out the user. handleRefreshToken can be used to refresh your user's session if it's matched with an external authentication service. handleRefreshToken should return an updated user object (i. e. with a new access token). It is not used by default, but you can override AuthenticationBackgroundComponent component to periodically refresh your session.
import { useCurrentAdmin } from'adminjs';constapi=newApiClient();// ...constAuthenticationBackgroundComponentOverride= () => {const [currentAdmin,setCurrentAdmin] =useCurrentAdmin();// ...// A part of your code responsible for refreshing user's sessionconstrequestBody= {};constresponse=awaitapi.refreshToken(requestBody);const { data } = response;setCurrentAdmin(data);// ...returnnull;}exportdefault AuthenticationBackgroundComponentOverride;