連番
対象
目的
やってみる
前提
- 機能を理解するのが目的なので、terraformは使わずにAWSコンソールからポチポチ
- ローカル環境はMac
準備
$ brew install npm
$ npm install -g @angular/cli
$ ng -v
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 1.7.4
Node: 10.0.0
OS: darwin x64
Angular:
...
雛形ファイル作成
- 雛形ファイル作成。エラーが出たので以下を追加した
- エラーの内容
$ ng g service service/cognito
node_modules appears empty, you may need to run `npm install`
$ ng new cognito-login --routing
$ cd cognito-login
$ npm install
$ ng g service service/cognito
$ ng g service service/pet
$ ng g component login
$ ng g component petlist
$ ng g component dashboard
$ npm i --save amazon-cognito-identity-js
$ npm i --save aws-sdk
プロジェクトの設定
$ vi src/tsconfig.app.json
"types": [
"node"
]
$ vi src/environments/enrivonment.ts
region
userPoolId
clientId
を追加
export const environment = {
production: false,
region: 'ap-northeast-1',
userPoolId: 'ap-northeast-1_xxxxxxxxx',
clientId: 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
};
$ vi src/app/app/module.ts
- サービスをimportして、
providers
を追加
// add service
import { CognitoService } from './service/cognito.service';
import { PetService } from './service/pet.service';
providers: [CognitoService, PetService],
Cognito認証処理の実装
$ vi src/app/service/cognito.service.ts
//ログイン処理
のnewPasswordRequired
ブロックで、completeNewPasswordChallenge
の1番目の引数は'NewPassword'
- ルーティングの実装
$ vi src/app/app-routing.module.ts
import { PetlistComponent } from './petlist/petlist.component';
import { LoginComponent } from './login/login.component';
const routes: Routes = [
{ path: '', redirectTo: '/login', pathMatch: 'full' },
{ path: 'petlist', component: PetlistComponent },
{
path: 'login',
component: LoginComponent,
pathMatch: 'full'
}
];
$ vi src/service/pet.service.ts
private Url = 'https://xxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/test/pets'; // URL to web api
- ここを見てコンポーネントの実装
src/app/app.components.*/
の中身実装
src/app/dashboard/
配下のファイルの中身実装
src/app/login/
配下のファイルの中身実装
src/app/petlist/
配下のファイルの中身実装
src/app/mock-test.ts
追加
src/app/pet.ts
追加
- モジュールの設定
$ vi src/app/app.module.ts
動作確認
$ ng server
http://localhost:4200/login
(解決)残課題1
"Unable to verify secret hash for client [アプリクライアントID]"
- ここを参照して、アプリクライアントにシークレットキーを使わないようにした
(解決)残課題2
User does not exist.
- わからん・・・
- (追記)処理フローを見たら使用しているのはusernameだった
- logoin.component.html内の
email
をusername
に変更
(解決)残課題3
Password does not conform to policy: Password must have numeric characters
- Cognito側のパスワードポリシーと違うな・・・
- (追記)作成したユーザは仮パスワード状態で、本パスワードが発行が必要っぽい
- (追記)
cognito.service.ts
内のnewPasswordRequired
で指定しているNewPassword
が本パスワードの指定箇所となるが、これがCognito User Poolのポリシーと異なっていた
newPasswordRequired: function (userAttributes, requiredAttributes) {
delete userAttributes.email_verified;
delete userAttributes.phone_number_verified;
cognitoUser.completeNewPasswordChallenge('Test@1001', userAttributes, this);
}
(pending)残課題4
ERROR TypeError: Cannot read property 'length' of undefined
at HttpHeaders.applyUpdate (webpack-internal:///./node_modules/@angular/common/esm5/http.js:362)
at eval (webpack-internal:///./node_modules/@angular/common/esm5/http.js:309)
at Array.forEach (<anonymous>)
at HttpHeaders.init (webpack-internal:///./node_modules/@angular/common/esm5/http.js:309)
at HttpHeaders.forEach (webpack-internal:///./node_modules/@angular/common/esm5/http.js:408)
at Observable.eval [as _subscribe] (webpack-internal:///./node_modules/@angular/common/esm5/http.js:2210)
at Observable._trySubscribe (webpack-internal:///./node_modules/rxjs/_esm5/Observable.js:177)
at Observable.subscribe (webpack-internal:///./node_modules/rxjs/_esm5/Observable.js:165)
at subscribeToResult (webpack-internal:///./node_modules/rxjs/_esm5/util/subscribeToResult.js:32)
src/app/service/pet.serivce.ts
を見ると、Pet
をAPI Gatewayから取っているっぽい・・・?
- 認証はできているので一旦終えて、次に進む!