0.2.4 • Published 3 months ago

@kinshipjs/lucia v0.2.4

Weekly downloads
-
License
(MIT OR Apache-2....
Repository
github
Last release
3 months ago

Kinship Logo Title & Description

Kinship Lucia Auth adapter

The Kinship Lucia Auth adapter serves as an adapter for Lucia Auth to provide an easy way to connect authentication schemes to your Kinship contexts.

Getting Started

Run the following commands.

npm i @kinshipjs/lucia
npm i @kinshipjs/mysql2 # or whichever adapter you prefer to use.

Create your database:

CREATE DATABASE auth;
USE auth;
CREATE TABLE User (
    Id VARCHAR(36) NOT NULL,
    FirstName VARCHAR(40) NOT NULL,
    LastName VARCHAR(40) NOT NULL,
    DateCreated DATETIME DEFAULT NOW(),
    DateModified DATETIME DEFAULT NOW() ON UPDATE NOW(),
    PRIMARY KEY (Id)
);

CREATE TABLE Role (
    Id VARCHAR(36) NOT NULL,
    Title VARCHAR(20) NOT NULL,
    Description VARCHAR(512) NOT NULL,
    DateCreated DATETIME DEFAULT NOW(),
    DateModified DATETIME DEFAULT NOW() ON UPDATE NOW(),
    PRIMARY KEY (Id)
);

CREATE TABLE xUserRole (
    UserId VARCHAR(36) NOT NULL,
    RoleId VARCHAR(36) NOT NULL,
    PRIMARY KEY (UserId, RoleId),
    FOREIGN KEY (UserId) REFERENCES User (Id),
    FOREIGN KEY (RoleId) REFERENCES Role (Id)
);

CREATE TABLE AuthSession (
    Id VARCHAR(256) NOT NULL,
    UserId VARCHAR(36) NOT NULL,
    ActiveExpires LONG NOT NULL,
    IdleExpires LONG NOT NULL,
    DateCreated DATETIME DEFAULT NOW(),
    DateModified DATETIME DEFAULT NOW() ON UPDATE NOW(),
    PRIMARY KEY (Id),
    FOREIGN KEY (UserId) REFERENCES User (Id)
);

CREATE TABLE AuthKey (
    Id VARCHAR(72) NOT NULL,
    UserId VARCHAR(36) NOT NULL,
    PrimaryKey BOOLEAN NOT NULL,
    HashedPassword VARCHAR(256),
    Expires INT,
    DateCreated DATETIME DEFAULT NOW(),
    DateModified DATETIME DEFAULT NOW() ON UPDATE NOW(),
    PRIMARY KEY (Id),
    FOREIGN KEY (UserId) REFERENCES User (Id)
);

Construct your TypeScript types:

export interface Historical {
    DateCreated?: Date;
    DateModified?: Date;
}

export interface User extends Historical {
    Id?: string;
    FirstName: string;
    LastName: string;
    Username: string;
    PassHash: string;

    xUserRoles?: xUserRole[];
    xEnvironmentUsers?: xEnvironmentUser[];
}

export interface Role extends Historical {
    Id?: string;
    Title: string;
    Description: string;

    xUserRoles?: xUserRole[];
}

export interface AuthSession extends Historical {
    Id?: string;
    UserId: string;
    ActiveExpires: number;
    IdleExpires: number;
}

export interface AuthKey extends Historical {
    Id?: string;
    UserId?: string;
    PrimaryKey: boolean;
    HashedPassword?: string;
    Expires?: number;
}

export interface xUserRole {
    UserId?: string;
    RoleId?: string;

    User?: User;
    Role?: Role;
}

Import @kinshipjs/core, @kinshipjs/mysql2, and @kinshipjs/lucia.

import { KinshipContext } from '@kinshipjs/core';
import { adapter, createMySql2Pool } from '@kinshipjs/mysql2';
import { adapter as luciaAdapter } from '@kinshipjs/lucia';

Configure your connection to your database.

const pool = createMySql2Pool({
    user: 'root',
    password: 'root',
    host: 'localhost',
    port: 3306,
    database:
});

const connection = adapter(pool);

Construct your KinshipContext objects.

const users = new KinshipContext<User>(connection, "User");
const userRoles = new KinshipContext<UserRoleXref>(connection, "UserRoleXref");
const roles = new KinshipContext<Role>(connection, "Role");
const keys = new KinshipContext<AuthKey>(connection, "AuthKey");
const sessions = new KinshipContext<AuthSession>(connection, "AuthSession");

Configure relationships (if any exist)

users.hasMany(m => m.UserRoles.fromTable("UserRoleXref").withKeys("Id", "UserId")
    .andThatHasOne(m => m.Role.withKeys("Id", "RoleId")));

Initialize lucia-auth.

import lucia from 'lucia-auth';
import { sveltekit } from 'lucia-auth/middleware'; // SvelteKit as an example
export const auth = lucia({
    middleware: sveltekit(),
    adapter: luciaAdapter({
        auth_key: keys,
        auth_session: sessions,
        auth_user: users
    }, {
        // lucia-auth expects the columns to be uniquely.
        auth_key: m => ({
            id: m.Id,
            user_id: m.UserId,
            hashed_password: m.HashedPassword,
            primary_key: m.PrimaryKey,
            expires: m.Expires
        }),
        auth_session: m => ({
            id: m.Id,
            user_id: m.UserId,
            idle_expires: m.IdleExpires,
            active_expires: m.ActiveExpires
        }),
        auth_user: m => ({
            id: m.Id
        })
    })
});

And you're done! (with the important part). For more information on what to do next with lucia-auth, you can visit their website here.

0.3.0-rc03

3 months ago

0.3.0-rc04

3 months ago

0.3.0-rc02

3 months ago

0.3.0-rc01

4 months ago

0.2.3

4 months ago

0.2.4

4 months ago

0.2.2

5 months ago

0.2.1

5 months ago

0.2.0

5 months ago

0.0.25-development

5 months ago

0.0.23-development

5 months ago

0.0.22-development

5 months ago

0.0.24-development

5 months ago

0.0.21-development

6 months ago

0.0.20-development

6 months ago

0.0.19-development

7 months ago

0.0.18-development

9 months ago

0.0.17-development

9 months ago

0.0.16-development

9 months ago

0.0.15-development

9 months ago

0.0.14-development

9 months ago

0.0.13-development

9 months ago

0.0.12-development

9 months ago

0.0.10-development

9 months ago

0.0.9-development

9 months ago

0.0.8-development

9 months ago

0.0.7-development

9 months ago

0.0.6-development

9 months ago

0.0.5-development

9 months ago

0.0.4-development

9 months ago

0.0.3-development

9 months ago

0.0.2-development

9 months ago

0.0.1-development

9 months ago