AWS Cognitoを使って、ログインの仕組みを作成し、ログイン後getCurrentUser()
を使ってログインユーザーを確認するところまでいけました.
ところが、ログイン後にページをリロードして再度getCurrentUser()
を実行するとnull
が返ってきました(つまりログインしていないことになっている)
getCurrentUser
自体はlocalStorageからデータを読み出すと聞いていますので、これが正しければページのリロードは
関係のないはずですし、localStorageを確認したところデータはしっかり入ったままでしたので原因が思いつきません。
どなたか詳しい方がいれば教えていただけるとありがたいです。
参考ですが、私の使っているログインとログインユーザー取得のコードが以下です。
【ログイン】
js
1 async login({ dispatch }, payload) 2 { 3 4 const authenticationDetails = new AuthenticationDetails({ 5 Username: payload.username, 6 Password: payload.password 7 }); 8 9 const userPool = new CognitoUserPool({ 10 UserPoolId: USER_POOL_ID, 11 ClientId : CLIENT_ID 12 }); 13 14 const cognitoUser = new CognitoUser({ 15 Username: payload.username, 16 Pool : userPool 17 }); 18 19 await new Promise((resolve, reject) => { 20 21 cognitoUser.authenticateUser(authenticationDetails, { 22 onSuccess: res => resolve(res), 23 onFailure: err => reject(err) 24 }) 25 26 }).catch(() => { 27 throw new ApplicationError() 28 }); 29 }
【ユーザー取得】
js
1 async getCognitoSession({ dispatch }) 2 { 3 const userPool = new CognitoUserPool({ 4 UserPoolId: USER_POOL_ID, 5 ClientId : CLIENT_ID 6 }); 7 8 const cognitoUser = userPool.getCurrentUser() 9 10 11 //ページリロード後にここでnullが出て引っかかってしまう。。。 12 if(!cognitoUser){ 13 return null 14 } 15 16 const session = await new Promise((resolve, reject) => { 17 18 cognitoUser.getSession((err, session) => { 19 err ? reject(err.message) : resolve(session) 20 }) 21 22 }).catch(() => { 23 return null 24 }); 25 26 return session 27 },
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/24 00:21