Migrate authorize{ actions, actionType, reducer} to TS & make factory service for interfaces #15

This commit is contained in:
Qolzam
2017-10-08 17:30:03 +07:00
parent f7c1a1ac00
commit 3b3899e7af
26 changed files with 652 additions and 218 deletions

View File

@@ -0,0 +1,112 @@
// - Import react components
import { firebaseRef, firebaseAuth } from 'app/firebase/';
import { IAuthorizeService } from "services/authorize";
import { User } from "Domain/users";
import { LoginUser, RegisterUserResult } from "domain/authorize";
import { SocialError } from "domain/common";
/**
* Firbase authorize service
*
* @export
* @class AuthorizeService
* @implements {IAuthorizeService}
*/
export class AuthorizeService implements IAuthorizeService {
/**
* Login the user
*
* @returns {Promise<LoginUser>}
* @memberof IAuthorizeService
*/
public login: (email: string, password: string) => Promise<LoginUser> = (email, password) => {
return new Promise<LoginUser>((resolve, reject) => {
firebaseAuth()
.signInWithEmailAndPassword(email, password)
.then((result) => {
resolve(new LoginUser(result.uid));
})
.catch((error: any) => {
reject(new SocialError(error.code, error.message));
})
});
};
/**
* Logs out the user
*
* @returns {Promise<void>}
* @memberof IAuthorizeService
*/
public logout: () => Promise<void> = () => {
return new Promise<void>((resolve, reject) => {
firebaseAuth()
.signOut()
.then((result) => {
resolve();
})
.catch((error: any) => {
reject(new SocialError(error.code, error.message));
})
});
}
/**
* Register a user
*
* @returns {Promise<void>}
* @memberof IAuthorizeService
*/
public registerUser: (user: User) => Promise<RegisterUserResult> = (user) => {
return new Promise<RegisterUserResult>((resolve, reject) => {
firebaseAuth()
.createUserWithEmailAndPassword(user.email, user.password)
.then((signupResult) => {
firebaseRef.child(`users/${signupResult.uid}/info`)
.set({
...user,
avatar: 'noImage'
})
.then((result) => {
resolve(new RegisterUserResult(signupResult.uid));
})
.catch((error: any) => reject(new SocialError(error.name, error.message)));
})
.catch((error: any) => reject(new SocialError(error.code, error.message)));
});
}
/**
* Update user password
*
* @returns {Promise<void>}
* @memberof IAuthorizeService
*/
public updatePassword: (newPassword: string) => Promise<void> = (newPassword) => {
console.log('====================================');
console.log("update password");
console.log('====================================');
return new Promise<void>((resolve, reject) => {
var user = firebaseAuth().currentUser;
console.log('====================================');
console.log(user);
console.log('====================================');
if (user) {
user.updatePassword(newPassword).then(() => {
// Update successful.
resolve();
}).catch((error: any) => {
// An error happened.
reject(new SocialError(error.code, error.message));
});
}
});
}
}