Migrate authorize{ actions, actionType, reducer} to TS & make factory service for interfaces #15
This commit is contained in:
@@ -1,139 +0,0 @@
|
|||||||
// - Import react components
|
|
||||||
import {firebaseRef, firebaseAuth} from 'app/firebase/'
|
|
||||||
import moment from 'moment'
|
|
||||||
import {push} from 'react-router-redux'
|
|
||||||
|
|
||||||
// - Import action types
|
|
||||||
import * as types from 'actionTypes'
|
|
||||||
|
|
||||||
// - Import actions
|
|
||||||
import * as globalActions from 'globalActions'
|
|
||||||
|
|
||||||
/* _____________ CRUD DB _____________ */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Log in user in server
|
|
||||||
* @param {string} email
|
|
||||||
* @param {string} password
|
|
||||||
*/
|
|
||||||
export var dbLogin = (email, password) => {
|
|
||||||
return (dispatch, getState) => {
|
|
||||||
dispatch(globalActions.showNotificationRequest())
|
|
||||||
|
|
||||||
return firebaseAuth().signInWithEmailAndPassword(email, password).then((result) => {
|
|
||||||
dispatch(globalActions.showNotificationSuccess())
|
|
||||||
dispatch(login(result.uid))
|
|
||||||
dispatch(push('/'))
|
|
||||||
}, (error) => dispatch(globalActions.showErrorMessage(error.code)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Log out user in server
|
|
||||||
*/
|
|
||||||
export var dbLogout = () => {
|
|
||||||
return (dispatch, getState) => {
|
|
||||||
return firebaseAuth().signOut().then((result) => {
|
|
||||||
dispatch(logout())
|
|
||||||
dispatch(push('/login'))
|
|
||||||
|
|
||||||
}, (error) => dispatch(globalActions.showErrorMessage(error.code)))
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Register user in database
|
|
||||||
* @param {object} user
|
|
||||||
*/
|
|
||||||
export var dbSignup = (user) => {
|
|
||||||
return (dispatch, getState) => {
|
|
||||||
dispatch(globalActions.showNotificationRequest())
|
|
||||||
return firebaseAuth().createUserWithEmailAndPassword(user.email, user.password).then((signupResult) => {
|
|
||||||
firebaseRef.child(`users/${signupResult.uid}/info`).set({
|
|
||||||
...user,
|
|
||||||
avatar:'noImage'
|
|
||||||
}).then((result) => {
|
|
||||||
|
|
||||||
dispatch(globalActions.showNotificationSuccess())
|
|
||||||
|
|
||||||
}, (error) => dispatch(globalActions.showErrorMessage(error.code)))
|
|
||||||
|
|
||||||
dispatch(signup({
|
|
||||||
uid: signupResult.uid,
|
|
||||||
...user
|
|
||||||
}))
|
|
||||||
dispatch(push('/'))
|
|
||||||
}, (error) => dispatch(globalActions.showErrorMessage(error.code)))
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Change user's password
|
|
||||||
* @param {string} newPassword
|
|
||||||
*/
|
|
||||||
export const dbUpdatePassword = (newPassword) => {
|
|
||||||
return (dispatch, getState) => {
|
|
||||||
dispatch(globalActions.showNotificationRequest())
|
|
||||||
firebaseAuth().onAuthStateChanged((user) => {
|
|
||||||
if (user) {
|
|
||||||
|
|
||||||
user.updatePassword(newPassword).then(() => {
|
|
||||||
// Update successful.
|
|
||||||
dispatch(globalActions.showNotificationSuccess())
|
|
||||||
dispatch(updatePassword())
|
|
||||||
dispatch(push('/'))
|
|
||||||
}, (error) => {
|
|
||||||
// An error happened.
|
|
||||||
switch (error.code) {
|
|
||||||
case 'auth/requires-recent-login':
|
|
||||||
dispatch(globalActions.showErrorMessage(error.code))
|
|
||||||
dispatch(dbLogout())
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* _____________ CRUD State _____________ */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loing user
|
|
||||||
* @param {string} uid
|
|
||||||
*/
|
|
||||||
export var login = (uid) => {
|
|
||||||
return {type: types.LOGIN, authed: true, uid}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Logout user
|
|
||||||
*/
|
|
||||||
export var logout = () => {
|
|
||||||
return {type: types.LOGOUT}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Register user
|
|
||||||
* @param {object} user
|
|
||||||
*/
|
|
||||||
export var signup = (user) => {
|
|
||||||
return {
|
|
||||||
type: types.SIGNUP,
|
|
||||||
...user
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Update user's password
|
|
||||||
*/
|
|
||||||
export const updatePassword = () => {
|
|
||||||
return {type: types.UPDATE_PASSWORD}
|
|
||||||
}
|
|
||||||
|
|
||||||
148
app/actions/authorizeActions.ts
Normal file
148
app/actions/authorizeActions.ts
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
// - Import react components
|
||||||
|
import { firebaseRef, firebaseAuth } from 'app/firebase/';
|
||||||
|
import moment from 'moment';
|
||||||
|
import { push } from 'react-router-redux';
|
||||||
|
|
||||||
|
// -Import domain
|
||||||
|
import { User } from 'domain/users';
|
||||||
|
import { SocialError } from "domain/common";
|
||||||
|
|
||||||
|
|
||||||
|
// - Import action types
|
||||||
|
import { AuthorizeActionType } from 'constants/authorizeActionType';
|
||||||
|
|
||||||
|
// - Import services
|
||||||
|
import { IAuthorizeService } from "services/authorize";
|
||||||
|
import { IServiceProvider } from "factories";
|
||||||
|
import { ServiceProvide } from "factories";
|
||||||
|
|
||||||
|
const serviceProvider: IServiceProvider = new ServiceProvide();
|
||||||
|
const authorizeService: IAuthorizeService = serviceProvider.createAuthorizeService();
|
||||||
|
|
||||||
|
// - Import actions
|
||||||
|
import * as globalActions from 'actions/globalActions';
|
||||||
|
|
||||||
|
|
||||||
|
/* _____________ CRUD DB _____________ */
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Log in user in server
|
||||||
|
* @param {string} email
|
||||||
|
* @param {string} password
|
||||||
|
*/
|
||||||
|
export const dbLogin = (email: string, password: string) => {
|
||||||
|
return (dispatch: any, getState: any) => {
|
||||||
|
dispatch(globalActions.showNotificationRequest())
|
||||||
|
|
||||||
|
return authorizeService.login(email, password).then((result) => {
|
||||||
|
dispatch(globalActions.showNotificationSuccess())
|
||||||
|
dispatch(login(result.uid))
|
||||||
|
dispatch(push('/'))
|
||||||
|
}, (error: SocialError) => dispatch(globalActions.showErrorMessage(error.code)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Log out user in server
|
||||||
|
*/
|
||||||
|
export const dbLogout = () => {
|
||||||
|
return (dispatch: any, getState: any) => {
|
||||||
|
return authorizeService.logout().then((result) => {
|
||||||
|
dispatch(logout());
|
||||||
|
dispatch(push('/login'));
|
||||||
|
|
||||||
|
}, (error: SocialError) => dispatch(globalActions.showErrorMessage(error.code)))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register user in database
|
||||||
|
* @param {object} user
|
||||||
|
*/
|
||||||
|
export const dbSignup = (user: User) => {
|
||||||
|
return (dispatch: any, getState: any) => {
|
||||||
|
dispatch(globalActions.showNotificationRequest())
|
||||||
|
return authorizeService.registerUser(user).then((result) => {
|
||||||
|
dispatch(signup({
|
||||||
|
userId: result.uid,
|
||||||
|
...user
|
||||||
|
}))
|
||||||
|
dispatch(push('/'))
|
||||||
|
})
|
||||||
|
.catch((error: SocialError) => dispatch(globalActions.showErrorMessage(error.code)));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Change user's password
|
||||||
|
* @param {string} newPassword
|
||||||
|
*/
|
||||||
|
export const dbUpdatePassword = (newPassword: string) => {
|
||||||
|
return (dispatch: any, getState: any) => {
|
||||||
|
dispatch(globalActions.showNotificationRequest())
|
||||||
|
|
||||||
|
return authorizeService.updatePassword(newPassword).then(() => {
|
||||||
|
|
||||||
|
// Update successful.
|
||||||
|
dispatch(globalActions.showNotificationSuccess())
|
||||||
|
dispatch(updatePassword())
|
||||||
|
dispatch(push('/'))
|
||||||
|
})
|
||||||
|
.catch((error: SocialError) => {
|
||||||
|
// An error happened.
|
||||||
|
switch (error.code) {
|
||||||
|
case 'auth/requires-recent-login':
|
||||||
|
dispatch(globalActions.showErrorMessage(error.code))
|
||||||
|
dispatch(dbLogout())
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* _____________ CRUD State _____________ */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loing user
|
||||||
|
* @param {string} uid
|
||||||
|
*/
|
||||||
|
export const login = (uid: string) => {
|
||||||
|
return {
|
||||||
|
type: AuthorizeActionType.LOGIN,
|
||||||
|
payload: { authed: true, uid }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logout user
|
||||||
|
*/
|
||||||
|
export const logout = () => {
|
||||||
|
return { type: AuthorizeActionType.LOGOUT }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register user
|
||||||
|
* @param {object} user
|
||||||
|
*/
|
||||||
|
export const signup = (user: User) => {
|
||||||
|
return {
|
||||||
|
type: AuthorizeActionType.SIGNUP,
|
||||||
|
payload: { ...user }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update user's password
|
||||||
|
*/
|
||||||
|
export const updatePassword = () => {
|
||||||
|
return { type: AuthorizeActionType.UPDATE_PASSWORD }
|
||||||
|
}
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@ import firebase, { firebaseRef } from '../firebase';
|
|||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
|
|
||||||
// - Import action types
|
// - Import action types
|
||||||
import { postActionType } from 'constants/postActionType';
|
import { PostActionType } from 'constants/postActionType';
|
||||||
|
|
||||||
// - Import actions
|
// - Import actions
|
||||||
import * as globalActions from 'actions/globalActions';
|
import * as globalActions from 'actions/globalActions';
|
||||||
@@ -278,7 +278,7 @@ export const dbGetPostsByUserId = (uid: string) => {
|
|||||||
*/
|
*/
|
||||||
export const addPost = (uid: string, post: any) => {
|
export const addPost = (uid: string, post: any) => {
|
||||||
return {
|
return {
|
||||||
type: postActionType.ADD_POST,
|
type: PostActionType.ADD_POST,
|
||||||
payload: { uid, post }
|
payload: { uid, post }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -290,7 +290,7 @@ export const addPost = (uid: string, post: any) => {
|
|||||||
*/
|
*/
|
||||||
export const updatePost = (uid: string, post: any) => {
|
export const updatePost = (uid: string, post: any) => {
|
||||||
return {
|
return {
|
||||||
type: postActionType.UPDATE_POST,
|
type: PostActionType.UPDATE_POST,
|
||||||
payload: { uid, post }
|
payload: { uid, post }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -302,7 +302,7 @@ export const updatePost = (uid: string, post: any) => {
|
|||||||
*/
|
*/
|
||||||
export const deletePost = (uid: string, id: string) => {
|
export const deletePost = (uid: string, id: string) => {
|
||||||
return {
|
return {
|
||||||
type: postActionType.DELETE_POST,
|
type: PostActionType.DELETE_POST,
|
||||||
payload: { uid, id }
|
payload: { uid, id }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -315,7 +315,7 @@ export const deletePost = (uid: string, id: string) => {
|
|||||||
*/
|
*/
|
||||||
export const addPosts = (uid: string, posts: any) => {
|
export const addPosts = (uid: string, posts: any) => {
|
||||||
return {
|
return {
|
||||||
type: postActionType.ADD_LIST_POST,
|
type: PostActionType.ADD_LIST_POST,
|
||||||
payload: { uid, posts }
|
payload: { uid, posts }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -325,7 +325,7 @@ export const addPosts = (uid: string, posts: any) => {
|
|||||||
*/
|
*/
|
||||||
export const clearAllData = () => {
|
export const clearAllData = () => {
|
||||||
return {
|
return {
|
||||||
type: postActionType.CLEAR_ALL_DATA_POST
|
type: PostActionType.CLEAR_ALL_DATA_POST
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -335,7 +335,7 @@ export const clearAllData = () => {
|
|||||||
*/
|
*/
|
||||||
export const addImagePost = (uid: string, post: any) => {
|
export const addImagePost = (uid: string, post: any) => {
|
||||||
return {
|
return {
|
||||||
type: postActionType.ADD_IMAGE_POST,
|
type: PostActionType.ADD_IMAGE_POST,
|
||||||
payload: { uid, post }
|
payload: { uid, post }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
7
app/constants/authorizeActionType.ts
Normal file
7
app/constants/authorizeActionType.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
export enum AuthorizeActionType
|
||||||
|
{
|
||||||
|
LOGIN = 'LOGIN',
|
||||||
|
LOGOUT = 'LOGOUT',
|
||||||
|
SIGNUP = 'SIGNUP',
|
||||||
|
UPDATE_PASSWORD = 'UPDATE_PASSWORD'
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
export enum postActionType {
|
export enum PostActionType {
|
||||||
ADD_IMAGE_POST = "ADD_IMAGE_POST",
|
ADD_IMAGE_POST = "ADD_IMAGE_POST",
|
||||||
ADD_VIDEO_POST = "ADD_VIDEO_POST",
|
ADD_VIDEO_POST = "ADD_VIDEO_POST",
|
||||||
ADD_POST = "ADD_POST",
|
ADD_POST = "ADD_POST",
|
||||||
@@ -7,5 +7,5 @@
|
|||||||
DELETE_POST = "DELETE_POST",
|
DELETE_POST = "DELETE_POST",
|
||||||
ADD_LIST_POST = "ADD_LIST_POST",
|
ADD_LIST_POST = "ADD_LIST_POST",
|
||||||
CLEAR_ALL_DATA_POST = "CLEAR_ALL_DATA_POST"
|
CLEAR_ALL_DATA_POST = "CLEAR_ALL_DATA_POST"
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|||||||
7
app/domain/authorize/index.ts
Normal file
7
app/domain/authorize/index.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import { LoginUser } from "./loginResult";
|
||||||
|
import { RegisterUserResult } from "./registerUserResult";
|
||||||
|
|
||||||
|
export {
|
||||||
|
LoginUser,
|
||||||
|
RegisterUserResult
|
||||||
|
}
|
||||||
25
app/domain/authorize/loginResult.ts
Normal file
25
app/domain/authorize/loginResult.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { BaseDomain } from "domain/common";
|
||||||
|
|
||||||
|
export class LoginUser extends BaseDomain{
|
||||||
|
|
||||||
|
constructor(uid: string){
|
||||||
|
super();
|
||||||
|
|
||||||
|
this._uid = uid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User identifier
|
||||||
|
*
|
||||||
|
* @type {string}
|
||||||
|
* @memberof LoginUser
|
||||||
|
*/
|
||||||
|
|
||||||
|
private _uid : string;
|
||||||
|
public get uid() : string {
|
||||||
|
return this._uid;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
23
app/domain/authorize/registerUserResult.ts
Normal file
23
app/domain/authorize/registerUserResult.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import { BaseDomain } from "domain/common";
|
||||||
|
|
||||||
|
export class RegisterUserResult extends BaseDomain{
|
||||||
|
|
||||||
|
constructor(uid: string){
|
||||||
|
super();
|
||||||
|
|
||||||
|
this._uid = uid;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* User identifier
|
||||||
|
*
|
||||||
|
* @type {string}
|
||||||
|
* @memberof LoginUser
|
||||||
|
*/
|
||||||
|
|
||||||
|
private _uid : string;
|
||||||
|
public get uid() : string {
|
||||||
|
return this._uid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
4
app/domain/common/baseDomain.ts
Normal file
4
app/domain/common/baseDomain.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
export class BaseDomain{
|
||||||
|
|
||||||
|
}
|
||||||
7
app/domain/common/index.ts
Normal file
7
app/domain/common/index.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import { SocialError } from "./socialError";
|
||||||
|
import { BaseDomain } from "./baseDomain";
|
||||||
|
|
||||||
|
export {
|
||||||
|
SocialError,
|
||||||
|
BaseDomain
|
||||||
|
}
|
||||||
45
app/domain/common/socialError.ts
Normal file
45
app/domain/common/socialError.ts
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
export class SocialError{
|
||||||
|
|
||||||
|
constructor(code: string, description: string){
|
||||||
|
this._code = code;
|
||||||
|
this._description = description;
|
||||||
|
this._isError = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error code
|
||||||
|
*
|
||||||
|
* @type {string}
|
||||||
|
* @memberof SocialError
|
||||||
|
*/
|
||||||
|
private _code : string;
|
||||||
|
public get code() : string {
|
||||||
|
return this._code;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error description
|
||||||
|
*
|
||||||
|
* @type {string}
|
||||||
|
* @memberof SocialError
|
||||||
|
*/
|
||||||
|
|
||||||
|
private _description : string;
|
||||||
|
public get description() : string {
|
||||||
|
return this._description;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If is error {true} if not {false}
|
||||||
|
*
|
||||||
|
* @type {Boolean}
|
||||||
|
* @memberof SocialError
|
||||||
|
*/
|
||||||
|
|
||||||
|
private _isError : Boolean;
|
||||||
|
public get isError() : Boolean {
|
||||||
|
return this._isError;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
5
app/domain/users/index.ts
Normal file
5
app/domain/users/index.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import User from './user';
|
||||||
|
|
||||||
|
export {
|
||||||
|
User
|
||||||
|
}
|
||||||
30
app/domain/users/user.ts
Normal file
30
app/domain/users/user.ts
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import { BaseDomain } from "domain/common";
|
||||||
|
|
||||||
|
class User extends BaseDomain {
|
||||||
|
/**
|
||||||
|
* Email of the user
|
||||||
|
*
|
||||||
|
* @type {string}
|
||||||
|
* @memberof User
|
||||||
|
*/
|
||||||
|
public email: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Password of the user
|
||||||
|
*
|
||||||
|
* @type {string}
|
||||||
|
* @memberof User
|
||||||
|
*/
|
||||||
|
public password: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User identifier
|
||||||
|
*
|
||||||
|
* @type {string}
|
||||||
|
* @memberof User
|
||||||
|
*/
|
||||||
|
public userId: string;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export default User;
|
||||||
10
app/factories/IServiceProvider.ts
Normal file
10
app/factories/IServiceProvider.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import {IAuthorizeService} from 'services/authorize/IAuthorizeService'
|
||||||
|
export interface IServiceProvider{
|
||||||
|
/**
|
||||||
|
* Create authorize service
|
||||||
|
*
|
||||||
|
* @memberof IServiceProvider
|
||||||
|
*/
|
||||||
|
createAuthorizeService : () => IAuthorizeService;
|
||||||
|
|
||||||
|
}
|
||||||
7
app/factories/index.ts
Normal file
7
app/factories/index.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import { IServiceProvider } from "./IServiceProvider";
|
||||||
|
import { ServiceProvide } from "./serviceProvide";
|
||||||
|
|
||||||
|
export {
|
||||||
|
IServiceProvider,
|
||||||
|
ServiceProvide
|
||||||
|
}
|
||||||
26
app/factories/serviceProvide.ts
Normal file
26
app/factories/serviceProvide.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
//#region Interfaces
|
||||||
|
|
||||||
|
import { IServiceProvider } from "factories";
|
||||||
|
import { IAuthorizeService } from "services/authorize";
|
||||||
|
|
||||||
|
//#endregion
|
||||||
|
|
||||||
|
//#region Service implemented classes
|
||||||
|
|
||||||
|
// - Firebase services
|
||||||
|
import { AuthorizeService } from "firebaseServices/authorize";
|
||||||
|
|
||||||
|
//#endregion
|
||||||
|
|
||||||
|
export class ServiceProvide implements IServiceProvider {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create instant for AuthorizeService
|
||||||
|
*
|
||||||
|
* @memberof ServiceProvide
|
||||||
|
*/
|
||||||
|
createAuthorizeService: () => IAuthorizeService = () => {
|
||||||
|
return new AuthorizeService();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
112
app/firebaseServices/authorize/AuthorizeService.ts
Normal file
112
app/firebaseServices/authorize/AuthorizeService.ts
Normal 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));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
5
app/firebaseServices/authorize/index.ts
Normal file
5
app/firebaseServices/authorize/index.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import { AuthorizeService } from "./AuthorizeService";
|
||||||
|
|
||||||
|
export {
|
||||||
|
AuthorizeService
|
||||||
|
}
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
// - Import action types
|
|
||||||
import * as types from 'actionTypes'
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default state
|
|
||||||
*/
|
|
||||||
var defaultState = {
|
|
||||||
uid: 0,
|
|
||||||
authed: false,
|
|
||||||
updatePassword: false,
|
|
||||||
guest:false
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Authorize reducer
|
|
||||||
* @param {object} state
|
|
||||||
* @param {object} action
|
|
||||||
*/
|
|
||||||
export var authorizeReducer = (state = defaultState, action) =>{
|
|
||||||
switch (action.type) {
|
|
||||||
case types.LOGIN:
|
|
||||||
return{
|
|
||||||
...state,
|
|
||||||
uid: action.uid,
|
|
||||||
authed: true,
|
|
||||||
guest:false
|
|
||||||
}
|
|
||||||
case types.LOGOUT:
|
|
||||||
return{
|
|
||||||
...state,
|
|
||||||
uid: 0,
|
|
||||||
authed: false,
|
|
||||||
guest:true
|
|
||||||
}
|
|
||||||
|
|
||||||
case types.SIGNUP:
|
|
||||||
return{
|
|
||||||
...state,
|
|
||||||
uid: action.uid
|
|
||||||
}
|
|
||||||
case types.UPDATE_PASSWORD:
|
|
||||||
return{
|
|
||||||
...state,
|
|
||||||
updatePassword: action.updatePassword
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return state
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
96
app/reducers/authorizeReducer.ts
Normal file
96
app/reducers/authorizeReducer.ts
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
// - Import react components
|
||||||
|
import {Reducer, Action} from "redux";
|
||||||
|
|
||||||
|
// - Import action types
|
||||||
|
import {AuthorizeActionType} from 'constants/authorizeActionType'
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default state
|
||||||
|
*/
|
||||||
|
var defaultState = {
|
||||||
|
uid: 0,
|
||||||
|
authed: false,
|
||||||
|
updatePassword: false,
|
||||||
|
guest:false
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default post state interface
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @interface IAuthorizeState
|
||||||
|
*/
|
||||||
|
export interface IAuthorizeState {
|
||||||
|
uid: number,
|
||||||
|
authed: Boolean,
|
||||||
|
updatePassword: Boolean,
|
||||||
|
guest:Boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default authorize action interface
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @interface IAuthorizeAction
|
||||||
|
*/
|
||||||
|
export interface IAuthorizeAction {
|
||||||
|
payload: any,
|
||||||
|
type: AuthorizeActionType
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default authorize reducer state
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @class DefaultAuthorizeState
|
||||||
|
* @implements {IAuthorizeState}
|
||||||
|
*/
|
||||||
|
export class DefaultAuthorizeState implements IAuthorizeState{
|
||||||
|
uid: number = 0;
|
||||||
|
authed: Boolean = false;
|
||||||
|
updatePassword: Boolean = false;
|
||||||
|
guest: Boolean = false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Authorize reducer
|
||||||
|
* @param {object} state
|
||||||
|
* @param {object} action
|
||||||
|
*/
|
||||||
|
export var authorizeReducer = (state : IAuthorizeState = new DefaultAuthorizeState(), action: IAuthorizeAction) =>{
|
||||||
|
const { payload } = action;
|
||||||
|
switch (action.type) {
|
||||||
|
case AuthorizeActionType.LOGIN:
|
||||||
|
return{
|
||||||
|
...state,
|
||||||
|
uid: payload.uid,
|
||||||
|
authed: true,
|
||||||
|
guest:false
|
||||||
|
}
|
||||||
|
case AuthorizeActionType.LOGOUT:
|
||||||
|
return{
|
||||||
|
...state,
|
||||||
|
uid: 0,
|
||||||
|
authed: false,
|
||||||
|
guest:true
|
||||||
|
}
|
||||||
|
|
||||||
|
case AuthorizeActionType.SIGNUP:
|
||||||
|
return{
|
||||||
|
...state,
|
||||||
|
uid: payload.userId
|
||||||
|
}
|
||||||
|
case AuthorizeActionType.UPDATE_PASSWORD:
|
||||||
|
return{
|
||||||
|
...state,
|
||||||
|
updatePassword: payload.updatePassword
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return state
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@ import {Reducer, Action} from "redux";
|
|||||||
|
|
||||||
|
|
||||||
// - Import action types
|
// - Import action types
|
||||||
import { postActionType } from "constants/postActionType";
|
import { PostActionType } from "constants/postActionType";
|
||||||
|
|
||||||
/* ---------------- */
|
/* ---------------- */
|
||||||
|
|
||||||
@@ -28,15 +28,22 @@ export interface IPostState {
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @export
|
* @export
|
||||||
* @interface postState
|
* @interface IPostAction
|
||||||
*/
|
*/
|
||||||
export interface IPostAction {
|
export interface IPostAction {
|
||||||
payload: any,
|
payload: any,
|
||||||
type: postActionType
|
type: PostActionType
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class defaultPostState implements IPostState{
|
/**
|
||||||
|
* Default post reducer state
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @class DefaultPostState
|
||||||
|
* @implements {IPostState}
|
||||||
|
*/
|
||||||
|
export class DefaultPostState implements IPostState{
|
||||||
userPosts: any = {};
|
userPosts: any = {};
|
||||||
loaded: boolean = false;
|
loaded: boolean = false;
|
||||||
|
|
||||||
@@ -47,13 +54,13 @@ export class defaultPostState implements IPostState{
|
|||||||
* @param {object} state
|
* @param {object} state
|
||||||
* @param {object} action
|
* @param {object} action
|
||||||
*/
|
*/
|
||||||
export var postReducer = (state : IPostState = new defaultPostState(), action : IPostAction) => {
|
export var postReducer = (state : IPostState = new DefaultPostState(), action : IPostAction) => {
|
||||||
const { payload } = action
|
const { payload } = action;
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case postActionType.CLEAR_ALL_DATA_POST:
|
case PostActionType.CLEAR_ALL_DATA_POST:
|
||||||
return new defaultPostState()
|
return new DefaultPostState()
|
||||||
|
|
||||||
case postActionType.ADD_IMAGE_POST:
|
case PostActionType.ADD_IMAGE_POST:
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
userPosts: {
|
userPosts: {
|
||||||
@@ -65,7 +72,7 @@ export var postReducer = (state : IPostState = new defaultPostState(), action :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case postActionType.ADD_POST:
|
case PostActionType.ADD_POST:
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
userPosts: {
|
userPosts: {
|
||||||
@@ -77,7 +84,7 @@ export var postReducer = (state : IPostState = new defaultPostState(), action :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case postActionType.UPDATE_POST:
|
case PostActionType.UPDATE_POST:
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
userPosts: {
|
userPosts: {
|
||||||
@@ -92,7 +99,7 @@ export var postReducer = (state : IPostState = new defaultPostState(), action :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case postActionType.DELETE_POST:
|
case PostActionType.DELETE_POST:
|
||||||
let filteredPosts = {}
|
let filteredPosts = {}
|
||||||
Object.keys(state.userPosts[payload.uid]).map((key) => {
|
Object.keys(state.userPosts[payload.uid]).map((key) => {
|
||||||
if (key !== payload.id) {
|
if (key !== payload.id) {
|
||||||
@@ -108,7 +115,7 @@ export var postReducer = (state : IPostState = new defaultPostState(), action :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case postActionType.ADD_LIST_POST:
|
case PostActionType.ADD_LIST_POST:
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
userPosts: {
|
userPosts: {
|
||||||
|
|||||||
41
app/services/authorize/IAuthorizeService.ts
Normal file
41
app/services/authorize/IAuthorizeService.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import { User } from "domain/users";
|
||||||
|
import { LoginUser, RegisterUserResult } from "domain/authorize";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Authentication service interface
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @interface IAuthorizeService
|
||||||
|
*/
|
||||||
|
export interface IAuthorizeService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Login the user
|
||||||
|
*
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
* @memberof IAuthorizeService
|
||||||
|
*/
|
||||||
|
login: (email: string, password: string) => Promise<LoginUser>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logs out the user
|
||||||
|
*
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
* @memberof IAuthorizeService
|
||||||
|
*/
|
||||||
|
logout: () => Promise<void>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
|
updatePassword: (newPassword: string) => Promise<void>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
|
registerUser: (user: User) => Promise<RegisterUserResult>;
|
||||||
|
|
||||||
|
}
|
||||||
5
app/services/authorize/index.ts
Normal file
5
app/services/authorize/index.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import { IAuthorizeService } from "./IAuthorizeService";
|
||||||
|
|
||||||
|
export {
|
||||||
|
IAuthorizeService
|
||||||
|
}
|
||||||
@@ -20,6 +20,7 @@
|
|||||||
"faker": "^4.1.0",
|
"faker": "^4.1.0",
|
||||||
"file-loader": "^0.11.1",
|
"file-loader": "^0.11.1",
|
||||||
"firebase": "^3.9.0",
|
"firebase": "^3.9.0",
|
||||||
|
"inversify": "^4.3.0",
|
||||||
"keycode": "^2.1.9",
|
"keycode": "^2.1.9",
|
||||||
"lodash": "^4.17.4",
|
"lodash": "^4.17.4",
|
||||||
"material-ui": "^0.19.3",
|
"material-ui": "^0.19.3",
|
||||||
@@ -44,6 +45,7 @@
|
|||||||
"redux": "^3.7.2",
|
"redux": "^3.7.2",
|
||||||
"redux-actions": "^2.0.3",
|
"redux-actions": "^2.0.3",
|
||||||
"redux-thunk": "^2.2.0",
|
"redux-thunk": "^2.2.0",
|
||||||
|
"reflect-metadata": "^0.1.10",
|
||||||
"sass-loader": "^6.0.3",
|
"sass-loader": "^6.0.3",
|
||||||
"save": "^2.3.0",
|
"save": "^2.3.0",
|
||||||
"script-loader": "^0.7.0",
|
"script-loader": "^0.7.0",
|
||||||
|
|||||||
@@ -1,18 +1,25 @@
|
|||||||
{
|
{
|
||||||
|
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"module": "es6", // use ES2015 modules
|
"module": "es6",
|
||||||
"target": "es6", // compile to ES2015 (Babel will do the rest)
|
"target": "es6",
|
||||||
|
"types": ["reflect-metadata"],
|
||||||
"allowSyntheticDefaultImports": true, // see below
|
"allowSyntheticDefaultImports": true, // see below
|
||||||
"baseUrl": "./app/", // enables you to import relative to this folder
|
"baseUrl": "./app/", // enables you to import relative to this folder
|
||||||
"paths": {
|
"paths": {
|
||||||
"app/*" : ["*"]
|
"app/*" : ["*"],
|
||||||
|
"domain/*" : ["domain/*"],
|
||||||
|
"factories/*" : ["factories/*"],
|
||||||
|
"services/*" : ["services/*"],
|
||||||
|
"firebaseServices/*" : ["firebaseServices/*"]
|
||||||
},
|
},
|
||||||
"sourceMap": true, // make TypeScript generate sourcemaps
|
"sourceMap": true, // make TypeScript generate sourcemaps
|
||||||
"outDir": "public", // output directory to build to (irrelevant because we use Webpack most of the time)
|
"outDir": "public", // output directory to build to (irrelevant because we use Webpack most of the time)
|
||||||
"jsx": "preserve", // enable JSX mode, but "preserve" tells TypeScript to not transform it (we'll use Babel)
|
"jsx": "preserve", // enable JSX mode, but "preserve" tells TypeScript to not transform it (we'll use Babel)
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"emitDecoratorMetadata": true,
|
||||||
"allowJs": true
|
"allowJs": true
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -72,7 +72,10 @@ module.exports = {
|
|||||||
path.resolve(__dirname, './app/api'),
|
path.resolve(__dirname, './app/api'),
|
||||||
path.resolve(__dirname, './app/constants'),
|
path.resolve(__dirname, './app/constants'),
|
||||||
path.resolve(__dirname, './app/actions'),
|
path.resolve(__dirname, './app/actions'),
|
||||||
path.resolve(__dirname, './app/reducers')
|
path.resolve(__dirname, './app/reducers'),
|
||||||
|
path.resolve(__dirname, './app/services'),
|
||||||
|
path.resolve(__dirname, './app/factories'),
|
||||||
|
path.resolve(__dirname, './app/domain')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -83,6 +86,10 @@ module.exports = {
|
|||||||
components: 'app/components',
|
components: 'app/components',
|
||||||
reducers: 'app/reducers',
|
reducers: 'app/reducers',
|
||||||
constants: 'app/constants',
|
constants: 'app/constants',
|
||||||
|
services: 'app/services',
|
||||||
|
factories: 'app/factories',
|
||||||
|
firebaseServices: 'app/firebaseServices',
|
||||||
|
domain: 'app/domain',
|
||||||
api: 'app/api',
|
api: 'app/api',
|
||||||
db: 'app/db',
|
db: 'app/db',
|
||||||
store: 'app/store',
|
store: 'app/store',
|
||||||
|
|||||||
Reference in New Issue
Block a user