Skip to content

Commit

Permalink
Avoid null check for User module interface
Browse files Browse the repository at this point in the history
  • Loading branch information
codeAesthetic committed Jun 11, 2021
1 parent ac4d4a0 commit d790dbe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
24 changes: 10 additions & 14 deletions src/modules/AuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,21 @@ const auth = firebaseApp.auth();
export function signUp({ email, password }: AuthFormData): Promise<User> {
return auth
.createUserWithEmailAndPassword(email, password)
.then((UserCredential) => {
const { user } = UserCredential;
if (user) {
return new User(user);
.then(({ user }) => {
if (user === null) {
throw new Error("sign-up failure");
}
throw new Error("User not found");
return new User(user);
});
}

export function signIn({ email, password }: AuthFormData): Promise<User> {
return auth
.signInWithEmailAndPassword(email, password)
.then((UserCredential) => {
const { user } = UserCredential;
if (user) {
return new User(user);
}
throw new Error("User not found");
});
return auth.signInWithEmailAndPassword(email, password).then(({ user }) => {
if (user === null) {
throw new Error("sign-in failure");
}
return new User(user);
});
}

export function requestPasswordReset(email: string): Promise<void> {
Expand Down
12 changes: 9 additions & 3 deletions src/modules/User.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import firebase from "firebase";

type getToken = Promise<string> | undefined;
type getToken = Promise<string>;

type IUSER = firebase.User;
interface Profile {
email: string | undefined | null;
email: string;
}

export class User {
Expand All @@ -16,12 +16,18 @@ export class User {

// getProfile will return email as of now
getProfile(): Profile {
if (this.user?.email === null) {
throw new Error("email not found");
}
return { email: this.user?.email };
}
/**
* Returns a JSON Web Token (JWT) used to identify the user to a Firebase service.
*/
getToken(): getToken {
return this.user?.getIdToken();
if (this.user?.getIdToken() === undefined) {
throw new Error("token not found");
}
return this.user.getIdToken();
}
}

0 comments on commit d790dbe

Please sign in to comment.