@dbcdk/login-nextjs
Advanced tools
DBC Provider for using login.bib.dk with nextJS
Weekly downloads
Readme
login-nextjs is a provider for the next-auth library, that can be used to setup authentication for nextJS applications through login.bib.dk.
npm i @dbcdk/login-nextjs
TODO
To add NextAuth.js to a project create a file called [...nextauth].js in pages/api/auth.
/**
* @file
* pages/api/[...nextauth].js
* */
import NextAuth from "next-auth";
import {adgangsplatformen, callbacks} from "@dbcdk/login-nextjs";
import CONFIG from "../config";
const options = {
providers: [
adgangsplatformen({
clientId: CONFIG.clientId,
clientSecret: CONFIG.clientSecret,
}),
],
callbacks: {
...callbacks,
},
};
export default (req, res) => NextAuth(req, res, options);
adgangspaltformen
is the provider function using login.bib.dk with next through next-auth and requires a ClientId
and clientSecret
The provider automatically uses a CULR ID (from login.bib.dk it is called uniqueId) as profile ID. If another ID should be used or further validation is needed a custom profile function can be used:
adgangsplatformen({
clientId: CONFIG.clientId,
clientSecret: CONFIG.clientSecret,
profile: ({id, profile}) => {
//custom logic here
return {id: 'some-valid-id'}
}
}),
callbacks
contains next-auth specific callbacks that will expose the token recieved from login.bib.dk and redirect to login.bib.dk/logout at logout (See the specific implementation here).
For more information about callbacks see https://next-auth.js.org/configuration/callbacks
The client library exposes two functions signIn
and signOut
that are wrappers around next-auth's signIn and signOut functions.
import {signIn} from "next-auth/client";
export default () => (
<button onClick={() => signIn()}>Sign in with login.bib.dk</button>
);
import {signOut} from "next-auth/client";
export default () => <button onClick={() => signOut()}>Sign out</button>;
npm run test