Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Angular+Auth0の話
Search
Hiroshi Oda
October 10, 2017
Technology
0
530
Angular+Auth0の話
ng-japan meetup 2017 AutumnのLT資料
Hiroshi Oda
October 10, 2017
Tweet
Share
More Decks by Hiroshi Oda
See All by Hiroshi Oda
グローバルIoTスタートアップにおける2年強に及ぶちょうど良いデータ基盤への歩み / Global IoT Startup Data Infrastructure
hi1280
0
1.1k
1人運用を支えるAmazon EKSノウハウ / Amazon EKS Know-How
hi1280
6
3.1k
ValidationMessageをいい感じにしようとした話
hi1280
1
610
Other Decks in Technology
See All in Technology
マルチモーダル / AI Agent / LLMOps 3つの技術トレンドで理解するLLMの今後の展望
hirosatogamo
37
12k
信頼性に挑む中で拡張できる・得られる1人のスキルセットとは?
ken5scal
2
530
[CV勉強会@関東 ECCV2024 読み会] オンラインマッピング x トラッキング MapTracker: Tracking with Strided Memory Fusion for Consistent Vector HD Mapping (Chen+, ECCV24)
abemii
0
220
社内で最大の技術的負債のリファクタリングに取り組んだお話し
kidooonn
1
550
AWS Lambda のトラブルシュートをしていて思うこと
kazzpapa3
2
170
Lambda10周年!Lambdaは何をもたらしたか
smt7174
2
110
OCI Network Firewall 概要
oracle4engineer
PRO
0
4.1k
The Rise of LLMOps
asei
5
1.3k
フルカイテン株式会社 採用資料
fullkaiten
0
40k
CysharpのOSS群から見るModern C#の現在地
neuecc
2
3.1k
スクラム成熟度セルフチェックツールを作って得た学びとその活用法
coincheck_recruit
1
140
Application Development WG Intro at AppDeveloperCon
salaboy
0
180
Featured
See All Featured
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Ruby is Unlike a Banana
tanoku
97
11k
How STYLIGHT went responsive
nonsquared
95
5.2k
The Cult of Friendly URLs
andyhume
78
6k
Raft: Consensus for Rubyists
vanstee
136
6.6k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
A designer walks into a library…
pauljervisheath
203
24k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
Testing 201, or: Great Expectations
jmmastey
38
7.1k
Building an army of robots
kneath
302
43k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
Transcript
Angular + Auth0ͷ খాത࢘ @hi1280 ng-japan meetup 2017 Autumn /
2017.10.10
খాത࢘(͓ͩͻΖ͠) @hi1280 • SE • Java • Angular
Auth0 • ೝূϓϥοτϑΥʔϜ • ֤छΞϓϦέʔγϣϯͱͷ࿈ܞ͕Մೳ(Angular ରԠ) • Technology RadarͰհ͞Ε͍ͯΔ
https://www.thoughtworks.com/radar/platforms
Angular + Auth0
࣍ • ϩάΠϯ • Ϣʔβใऔಘ • API࣮ߦ • ೝՄ •
Ϣʔβཧ
ϩάΠϯ
// ུ import * as auth0 from 'auth0-js'; @Injectable() export
class AuthService { auth0 = new auth0.WebAuth({ clientID: AUTH_CONFIG.clientID, domain: AUTH_CONFIG.domain, responseType: 'token id_token’, audience: AUTH_CONFIG.apiUrl, redirectUri: AUTH_CONFIG.callbackURL, scope: 'openid' }); public login(): void { this.auth0.authorize(); } } ೝূͷઃఆ ϩάΠϯը໘ىಈ
• Facebook • GitHub • Google • Twitter • MS
Account ͳͲ Ϣʔβ໊+ύεϫʔυೝূ͋Δ IDϓϩόΠμʔ
ϩάΠϯೝূͷϑϩʔ (1)Auth0ͷϩάΠϯը໘͕ݺΕΔ (2)ίʔϧόοΫͷURL͕ݺΕΔ • Auth0ͷཧը໘ͰίʔϧόοΫURLͷڐՄ͕ඞཁ (3)ϧʔτίϯϙʔωϯτͷίϯετϥΫλ͕ݺΕΔ
ϩάΠϯೝূͷϑϩʔ (1)Auth0ͷϩάΠϯը໘͕ݺΕΔ (2)ίʔϧόοΫͷURL͕ݺΕΔ • Auth0ͷཧը໘ͰίʔϧόοΫURLͷڐՄ͕ඞཁ (3)ϧʔτίϯϙʔωϯτͷίϯετϥΫλ͕ݺΕΔ ৄ͘͠Quick Start Λݟ͍ͯͩ͘͞ https://auth0.com/docs/
quickstart/spa/angular2
Ϣʔβใऔಘ
public getProfile(cb): void { const accessToken = localStorage.getItem('access_token'); if (!accessToken)
{ throw new Error('Access token must exist to fetch profile'); } const self = this; this.auth0.client.userInfo(accessToken, (err, profile) => { if (profile) { self.userProfile = profile; } cb(err, profile); }); }
public getProfile(cb): void { const accessToken = localStorage.getItem('access_token'); if (!accessToken)
{ throw new Error('Access token must exist to fetch profile'); } const self = this; this.auth0.client.userInfo(accessToken, (err, profile) => { if (profile) { self.userProfile = profile; } cb(err, profile); }); } ৄ͘͠Quick Start Λݟ͍ͯͩ͘͞ https://auth0.com/docs/ quickstart/spa/angular2
API࣮ߦ
API࣮ߦ • JWTΛHTTPϦΫΤετʹؚΊΔϞδϡʔϧ • HttpClientͷInterceptorͱ࣮ͯ͠͞Ε͍ͯΔ
import { JwtModule } from '@auth0/angular-jwt'; // ུ @NgModule({ //
ུ imports: [ // ུ JwtModule.forRoot({ config: { tokenGetter: getAccessToken, whitelistedDomains: ['localhost:3001'] } }) ], // ུ bootstrap: [AppComponent] }) export class AppModule { } export function getAccessToken() { return localStorage.getItem('access_token'); }
import { JwtModule } from '@auth0/angular-jwt'; // ུ @NgModule({ //
ུ imports: [ // ུ JwtModule.forRoot({ config: { tokenGetter: getAccessToken, whitelistedDomains: ['localhost:3001'] } }) ], // ུ bootstrap: [AppComponent] }) export class AppModule { } export function getAccessToken() { return localStorage.getItem('access_token'); } ৄ͘͠Quick Start Λݟ͍ͯͩ͘͞ https://auth0.com/docs/ quickstart/spa/angular2 ͱ @auth0/angular-jwt https://github.com/auth0/ angular2-jwt/tree/v1.0
ೝՄ
ೝՄ • RouterͷguardͰೝূࡁΈ͔Λఆ • Ϣʔβʹ༩͞ΕͨείʔϓͰΞΫηε੍ޚ • Auth0ͷRuleͰϢʔβͷείʔϓΛઃఆ
import { Injectable } from '@angular/core'; import { Router, CanActivate,
ActivatedRouteSnapshot } from '@angular/router'; import { AuthService } from './auth.service'; @Injectable() export class ScopeGuardService implements CanActivate { constructor(public auth: AuthService, public router: Router) { } canActivate(route: ActivatedRouteSnapshot): boolean { const scopes = (route.data as any).expectedScopes; if (!this.auth.isAuthenticated() || !this.auth.userHasScopes(scopes)) { this.router.navigate(['']); return false; } return true; } }
import { Injectable } from '@angular/core'; import { Router, CanActivate,
ActivatedRouteSnapshot } from '@angular/router'; import { AuthService } from './auth.service'; @Injectable() export class ScopeGuardService implements CanActivate { constructor(public auth: AuthService, public router: Router) { } canActivate(route: ActivatedRouteSnapshot): boolean { const scopes = (route.data as any).expectedScopes; if (!this.auth.isAuthenticated() || !this.auth.userHasScopes(scopes)) { this.router.navigate(['']); return false; } return true; } } ৄ͘͠Quick Start Λݟ͍ͯͩ͘͞ https://auth0.com/docs/ quickstart/spa/angular2
Ϣʔβཧ
Ϣʔβཧ • ϥΠϒϥϦͷαϙʔτ͕ෆे • Auth0ͷManagement APIΛݺͿ
Ϣʔβཧ • ϥΠϒϥϦͷαϙʔτ͕ෆे • Auth0ͷManagement APIΛݺͿ ৄ͘͠Management APIͷ υΩϡϝϯτΛݟ͍ͯͩ͘͞ https://auth0.com/docs/
api/management/v2/ tokens
Ҏ্