auth0認証とは
auth0のサービス
Auth0: Identity is Complex. Deal with it.
auth0概要(フリープラン)
- 無料で始めて、AUTH0で時間を節約しましょう!
- 7,000の無料アクティブユーザーと無制限のログイン
- パスワードなし
- Web、iOS、Android用のロック
- 最大2つのソーシャルIDプロバイダー
- 無制限のサーバーレスルール
auth0の実装方法
frontで実装できる技術は下記から選択することが可能です。
backendからの実装は下記の技術から選択することが可能です。今回はexpressを利用して実装したいと思います。
実装サンプル
const express = require('express'); const app = express(); const jwt = require('express-jwt'); const jwtAuthz = require('express-jwt-authz'); const jwksRsa = require('jwks-rsa'); const cors = require('cors'); require('dotenv').config(); if (!process.env.AUTH0_DOMAIN || !process.env.AUTH0_AUDIENCE) { throw 'Make sure you have AUTH0_DOMAIN, and AUTH0_AUDIENCE in your .env file'; } const corsOptions = { origin: 'http://localhost:3000' }; app.use(cors(corsOptions)); const checkJwt = jwt({ // Dynamically provide a signing key based on the [Key ID](https://tools.ietf.org/html/rfc7515#section-4.1.4) header parameter ("kid") and the signing keys provided by the JWKS endpoint. secret: jwksRsa.expressJwtSecret({ cache: true, rateLimit: true, jwksRequestsPerMinute: 5, jwksUri: `https://${process.env.AUTH0_DOMAIN}/.well-known/jwks.json` }), // Validate the audience and the issuer. audience: process.env.AUTH0_AUDIENCE, issuer: `https://${process.env.AUTH0_DOMAIN}/`, algorithms: ['RS256'] }); const checkScopes = jwtAuthz(['read:messages']); app.get('/api/public', function(req, res) { res.json({ message: 'Hello from a public endpoint! You don\'t need to be authenticated to see this.' }); }); app.get('/api/private', checkJwt, function(req, res) { res.json({ message: 'Hello from a private endpoint! You need to be authenticated to see this.' }); }); app.get('/api/private-scoped', checkJwt, checkScopes, function(req, res) { res.json({ message: 'Hello from a private endpoint! You need to be authenticated and have a scope of read:messages to see this.' }); }); app.use(function(err, req, res, next){ console.error(err.stack); return res.status(err.status).json({ message: err.message }); }); app.listen(3010); console.log('Listening on http://localhost:3010');