Default Adapter
デフォルトアダプタはMongoDBを簡単なドキュメント保存レイヤーとして使用して、ワンタイムトークンや リフレッシュトークンなどの標準的な認証機能を実装しています。
機能
- ユーザー認証
- ユーザー名とパスワードを使用した安全なユーザーログイン
- SMS、メール、認証アプリなどのオプションを含むマルチファクター認証(MFA)のサポート
- トークンの作成と検証
- APIリクエストで、JSON Web Token (JWT) を使用してアクセストークンを生成、認証を行います
- 発行したトークンを解析し、中の情報を返す
- SNSユーザー認証
- 設定したら、ユーザーがGoogle、Twitter、LineなどのSNSアカウントでシステムにログインできます
インストール手順
- 前提条件
パッケージ | バージョン |
---|---|
node | 18+ |
MongoDB | 5+ |
Nodeblocks User Service | 1.1.0+ |
- パッケージをインストール
リポジトリを作成して、このパッケージを追加
mkdir my-auth-service
npx gts init -y
npm install --save @basaldev/blocks-auth-service
環境関数を設置する必要があります。クイックスタートガイドをサンプルとして参照してください。
- コードを導入
備考
下記の例はCookie認証のサンプルです:
authenticate: security.defaultCookieAuth, // <-- Cookie認証
備考
下記の例はCORSの許可リストにlocalhostを追加する例です。お使いになる予定のドメインを配列に追加してください。
corsOptions: {
credentials: true,
origin: ['http://localhost', 'http://your-domain.com'],
},
src/index.tsに下記を入れてください:
import {
createNodeblocksAuthApp,
defaultAdapter,
ServiceOptsWithOAuth,
} from '@basaldev/blocks-auth-service';
import {getEnvBool, getEnvString} from './helper/utilities';
const authPort = Number(getEnvString('PORT'));
async function main() {
const oauthConfigs: ServiceOptsWithOAuth['oauthConfigs'] = {
google: {
clientId: getEnvString('OAUTH_GOOGLE_CLIENT_ID', ''),
clientSecret: getEnvString('OAUTH_GOOGLE_CLIENT_SECRET', ''),
scope: ['email', 'profile'],
},
};
const adapterOptions: defaultAdapter.AuthDefaultAdapterOptions = {
authEncSecret: getEnvString('AUTH_ENC_SECRET', ''),
authSignSecret: getEnvString('AUTH_SIGN_SECRET', ''),
/** ここにURLを追加してください */
authorizedRedirectUrls: [
'https://localhost:5173/auth/post-oauth-login',
],
authType: 'cookie',
enableRefreshToken: getEnvBool('ENABLE_REFRESH_TOKEN', false),
tokenExpireTime: {
accessToken: getEnvString('JWT_EXPIRATION_TIME', ''),
onetimeToken: getEnvString('JWT_EXPIRATION_TIME', ''),
refreshToken: getEnvString('JWT_EXPIRATION_TIME', ''),
},
};
const adapter = await defaultAdapter.createAuthDefaultAdapter(
adapterOptions,
{
db: getEnvString('DATABASE_URL', ''),
userAPI: getEnvString('USER_ENDPOINT', ''),
}
);
const app = await createNodeblocksAuthApp({
enableCookieParser: true,
corsOptions: {
credentials: true,
origin: ['http://localhost'],
},
});
await app.startService({
PORT: authPort,
adapter,
env: 'development',
/** ここにドメイン名を追加してください */
domain: '',
oauthConfigs,
});
}
void main();
SNSログインの場合は下記のように修正してください
nodeblocksAuthApp.startService({
domain: `auth-service-domain`, // remove the https:// part
oauthConfigs: { // Requires ClientID and ClientService from Line, google, and a callbackURL
// callback url examples: {auth-service-endpoint}/auth/line/callback, {auth-service-endpoint}/auth/google/callback
line: {
clientId: LINE_CLIENT_ID,
clientSecret: LINE_CLIENT_SECRET,
},
google: {
clientId: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
}
},
adapter: new AUTH_SERVICE.AuthDefaultAdapter({
dbUrl: DATABASE_URL,
userServiceEndpoint: USER_SERVICE_ENDPOINT,
jwtExpirationTime: JWT_EXPIRATION_TIME,
authEncSecret: AUTH_ENC_SECRET,
authSignSecret: AUTH_SIGN_SECRET,
authorizedRedirectUrls: '' // ->supply with frontend urls to redirect after login
// examples: {tanty-supplyside-endpoint}/login-callback/google,{tanty-demandside-endpoint}/login-callback/google,{tanty-demandside-endpoint}/login-callback/google,{tanty-supplyside-endpoint}/login-callback/line,
}),
PORT
})