私が使っているもの
私が持っているもの
core.es5.js:1020エラーO {コード: "auth / wrong-password"、メッセージ: "パスワードが無効であるか、ユーザーがパスワードを持っていません。"}
質問
私の認証サービス
updateEmailAddress(email: string, password: string) {
const currentUser = firebase.auth().currentUser;
const credentials = firebase.auth.EmailAuthProvider.credential(currentUser.email, password);
currentUser.reauthenticateWithCredential(credentials).then(function () {
currentUser.updateEmail(email).then(function () {
currentUser.sendEmailVerification().then(function () {
// Email Sent
}).catch(function (error) {
// An error happened.
});
}).catch(function (error) {
// An error happened.
});
});
}
更新
import { Injectable } from '@angular/core';
import { AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase/app';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
@Injectable()
export class AuthService {
user: Observable<firebase.User>;
redirectUrl: string;
constructor(
private firebaseAuth: AngularFireAuth,
private router: Router) {
this.user = firebaseAuth.authState;
}
// Login Form
login(email: string, password: string): Observable<boolean> {
this.firebaseAuth.auth
.signInWithEmailAndPassword(email, password)
.then(value => {
this.router.navigate(['projects']);
return Observable.of(true);
})
.catch(err => {
console.log('Something went wrong:', err.message);
return Observable.of(false);
});
return;
}
// Account Management - Re-authenticate the user using the current email and current password, then call the update email and email verification methods
updateEmailAddress(email: string, password: string) {
const currentUser = firebase.auth().currentUser;
const credentials = firebase.auth.EmailAuthProvider.credential(currentUser.email, password);
currentUser.reauthenticateWithCredential(credentials).then(function () {
currentUser.updateEmail(email).then(function () {
currentUser.sendEmailVerification().then(function () {
// Email Sent
}).catch(function (error) {
// An error happened.
});
}).catch(function (error) {
// An error happened.
alert(ErroAuthEn.convertMessage(error['code']));
});
});
}
// Log out of Apollo
logout() {
this.firebaseAuth.auth.signOut();
this.router.navigate(['login']);
}
// A method to use to check if the user is logged in or not
isLoggedIn(): boolean {
console.log(!!this.firebaseAuth.auth.currentUser);
return !!this.firebaseAuth.auth.currentUser;
}
}
export namespace ErroAuthEn {
export function convertMessage(code: string): string {
console.log('called');
switch (code) {
case 'auth/user-disabled': {
return 'Sorry your user is disabled.';
}
case 'auth/user-not-found': {
return 'Sorry user not found.';
}
case 'auth/wrong-password': {
return 'Sorry, incorrect password entered. Please try again.';
}
default: {
return 'Login error try again later.';
}
}
}
}
最善の解決策は、ドキュメントのエラーメッセージごとに、より理解しやすいユーザーフレンドリーなメッセージを返す関数を実装することです。
export namespace ErroAuthEn {
export function convertMessage(code: string): string {
switch (code) {
case 'auth/user-disabled': {
return 'Sorry your user is disabled.';
}
case 'auth/user-not-found': {
return 'Sorry user not found.';
}
.
.
.
default: {
return 'Login error try again later.';
}
}
}
}
次に、コンポーネントで次のように呼び出すことができます。
updateEmailAddress(email: string, password: string) {
const currentUser = firebase.auth().currentUser;
const credentials = firebase.auth.EmailAuthProvider.credential(currentUser.email, password);
currentUser.reauthenticateWithCredential(credentials).then(function () {
currentUser.updateEmail(email).then(function () {
currentUser.sendEmailVerification().then(function () {
// Email Sent
}).catch(function (error) {
// An error happened.
});
}).catch(function (error) {
alert( ErroAuthEn.convertMessage(error['code']));
});
});
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加