Getting Started

Get Started

Installation

Install the core module.

npm install @aponia.js/core

Initialize a session plugin.

src/auth/session.ts
import { JwtSessionPlugin } from '@aponia.js/core/plugins/session/jwt'
 
export const jwtSession = new JwtSessionPlugin()

Create providers.

src/auth/google.ts
import { OIDCProvider } from '@aponia.js/core/plugins/providers/oidc'
 
export const google = new OIDCProvider({
  id: 'google',
  clientId: process.env['GOOGLE_ID',
  clientSecret: process.env['GOOGLE_SECRET'],
  issuer: 'https://accounts.google.com',
  endpoints: {
    authorization: {
      params: {
        client_id: process.env['GOOGLE_ID'],
        response_type: 'code',
        scope: 'openid profile email',
      },
    },
  },
})

Create an adapter.

src/auth/adapter.ts
import { PrismaClient } from '@prisma/client'
import { AdapterPlugin, type Adapter } from '@aponia.js/core/adapter'
 
const prisma = new PrismaClient()
 
const rawAdapter: Adapter = {
  findAccount: async (_request, response) => {
    return await prisma.account.findFirst({
      where: {
        providerId: response.providerId,
        providerAccountId: response.providerAccountId,
      },
    })
  },
  getUserFromAccount: async (account, _request, _response) => {
    return await prisma.user.findFirst({
      where: {
        id: account.userId,
      },
    })
  },
  createSession: async (user, _account, _request, _response) => {
    return await prisma.session.create({
      userId: user.id,
      expires: Date.now() + 1000 * 60 * 24,
    })
  },
  createUser: async (_request, response) => {
    return await prisma.user.create({
      name: response.account.name,
      avatar: response.account.picture ?? response.account['image'],
    })
  },
  findUserAccounts: async (user, _request, _response) => {
    return await prisma.account.findMany({
      where: {
        userId: user.id,
      },
    })
  },
  createAccount: async (user, _request, response) => {
    return await prisma.account.create({
      providerId: response.providerId,
      providerAccountId: response.providerAccountId,
      userId: user.id,
    })
  },
}
 
export const adapter = new AdapterPlugin(rawAdapter)

Create an Auth instance with the session, adapter, and providers.

src/auth/index.ts
import { Auth } from '@aponia.js/core'
import { jwtSession } from './session'
import { google } from './google'
import { adapter } from './adapter'
 
export const auth = new Auth({
  plugins: [jwtSession, google, adapter]
})

Use the Auth instance in your app!

Plugins

There are three types of plugins provided by the core library.

Adapters

Adapters are made up of methods that you write yourself and can call during your authentication flows. Converting an Adapter to an AdapterPlugin registers a post-request handler that handles any authentication information created, i.e. after providers oversee a successful login.

Aponia.js does not provide pre-defined adapters. Recipes for different combinations of schemas, databases, and ORMs are provided to get started.