Default Repositories

SDK includes default repositories for accessing and saving data in resource server.

These repositories link to your database using the SDK's Datasource. Some repositories have dependencies on other repositories.

Since the SDK does not run any database migrations, you need to make sure that the following database structures are applied.

📌 Info

Column names are variable and adjustable during repository initialization. Feel free to use whatever name for your columns.

NameTypeRequiredNullableUniqueDescription
idanyYesNoYesUnique Identifier of the user
hashstringNoYesYesIdentifier to be used in user profile url instead of the real user id
namestringNoYesNoUser's full name
display_namestringNoYesNoUser's display name
given_namestringNoYesNo
family_namestringNoYesNo
usernamestringMaybeYesYesRequired only with username authentication enabled
emailstringMaybeYesYesRequired only with email authentication enabled
email_verified_attimestampMaybeYesNoRequired only with email authentication enabled
phonestringMaybeYesYesRequired only with phone authentication enabled
phone_verified_attimestampMaybeYesNoRequired only with phone authentication enabled
passwordstringMaybeYesNoNot required when system relies only on OTP login
picturestringNoYesNoUser's profile picture url
metadatajsonNoYesNoKeeps all UserEntity data
NameTypeRequiredNullableUniqueDescription
providerstringYesNoNoThe provider name (google, facebook, etc.)
identifierstringYesNoNoUser id in the social provider platform
user_idanyYesNONoInternal user id

Start by establishing a connection to the database. The complete list of supported connections may be found in the Datasource manual.

import { Datasource } from "@ssofy/node-sdk";
import mysql from "mysql2/promise";
...
const pool = mysql.createPool({
    host: 'localhost',
    port: 3306,
    user: 'root',
    password: 'root',
    database: 'db',
    waitForConnections: true,
    connectionLimit: 10,
    maxIdle: 10,
    idleTimeout: 60000,
    queueLimit: 0,
    enableKeepAlive: true,
    keepAliveInitialDelay: 0
});

const connection = new Datasource.MySQLPoolConnection(pool)

We may also need a temporary or persistent storage for keeping our temporary user tokens and otp codes.

import { Storage } from "@ssofy/node-sdk";
...
const storage = new Storage.MemoryStorage();

We need a user transformer too.

import { Transformers } from "@ssofy/node-sdk";
...
const userTranformer = new Transformers.DefaultUserTransformer();

Repositories:

import { Repositories, Models } from "@ssofy/node-sdk";
...
const socialLinkRepository = new Repositories.DefaultSocialLinkRepository(
    connection, 
    'social_links', // table name (or object when using ORM)
    { // custom column names map
        // identifier: 'provider_id' 
    }
);

const userRepository = new Repositories.DefaultUserRepository(
    connection,
    'users', // table name (or object when using ORM)
    storage,
    socialLinkRepository,
    userTransformer,
    { // custom column names map
        phone: 'phone_number'
    }
);

const clientRepository = new Repositories.DefaultClientRepository([
    <Models.ClientEntity>{
        id: 'test',
        name: 'Test',
        secret: 'test',
        redirect_uris: ['*'],
    },
]);

const scopeRepository = new Repositories.DefaultScopeRepository([
    <Models.ScopeEntity>{
        id: '*',
        title: 'All profile data',
    },
]);

const otpRepository = new Repositories.DefaultOTPRepository(
    storage,
    userRepository
);

Checkout the repository interfaces for the list of available repository methods.

ssofy Knowledge Base
At our core, we believe that staying up-to-date with the latest trends and advancements in Authentication and related areas is essential. That's why we take great pride in keeping you informed with the latest insights, updates, and news in this rapidly evolving landscape.


Do you need support?
SSOfy is by Cubelet Ltd.
Copyright © 2024 Cubelet Ltd. All rights reserved.