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.
Column names are variable and adjustable during repository initialization. Feel free to use whatever name for your columns.
Name | Type | Required | Nullable | Unique | Description |
---|---|---|---|---|---|
id | any | Yes | No | Yes | Unique Identifier of the user |
hash | string | No | Yes | Yes | Identifier to be used in user profile url instead of the real user id |
name | string | No | Yes | No | User's full name |
display_name | string | No | Yes | No | User's display name |
given_name | string | No | Yes | No | |
family_name | string | No | Yes | No | |
username | string | Maybe | Yes | Yes | Required only with username authentication enabled |
string | Maybe | Yes | Yes | Required only with email authentication enabled | |
email_verified_at | timestamp | Maybe | Yes | No | Required only with email authentication enabled |
phone | string | Maybe | Yes | Yes | Required only with phone authentication enabled |
phone_verified_at | timestamp | Maybe | Yes | No | Required only with phone authentication enabled |
password | string | Maybe | Yes | No | Not required when system relies only on OTP login |
picture | string | No | Yes | No | User's profile picture url |
metadata | json | No | Yes | No | Keeps all UserEntity data |
Name | Type | Required | Nullable | Unique | Description |
---|---|---|---|---|---|
provider | string | Yes | No | No | The provider name (google, facebook, etc.) |
identifier | string | Yes | No | No | User id in the social provider platform |
user_id | any | Yes | NO | No | Internal 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.