Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { observable, action } from 'mobx';
- import AsyncStorage from '@react-native-community/async-storage';
- import UserStore from './User';
- import { signupUser, loginUser, activateUserAccount, logutUser } from '../../endpoints/user';
- import { strings } from '../../utils/constants';
- export class Auth {
- @observable signupSuccessful = false;
- @observable signupErrors = null;
- @observable activationError = null;
- @observable loginError = null;
- @action signup(isPatient, signupData) {
- this.signupErrors = {};
- signupUser(isPatient, { ...signupData, mobile: true })
- .then(() => {
- this.signupSuccessful = true;
- })
- .catch(({ response: { status, data } }) => {
- if (status === 400) {
- this.setSignupErrors((data.error && data.error.errors) || {});
- } else if (status === 500) {
- this.setSignupErrors({ email: { message: strings.validationErrorSameEmail } });
- }
- });
- }
- @action login(loginData) {
- this.loginError = null;
- loginUser(loginData)
- .then(({ data }) => {
- if (data) {
- AsyncStorage.setItem('token', data.token);
- UserStore.setUserData(data);
- }
- })
- .catch(({ response: { status, data } }) => {
- if (status === 401) {
- this.setLoginError(data.message);
- }
- });
- }
- @action activateAccount(token) {
- activateUserAccount(token)
- .then()
- .catch(({ response }) => {
- if (response.data) {
- this.setActivationError(response.data.message);
- }
- });
- }
- @action async logout() {
- await AsyncStorage.removeItem('token');
- logutUser({ userId: this.userId })
- .then(() => {
- this.clear();
- UserStore.clear();
- })
- .catch(error => console.log({ error }));
- }
- @action setSignupErrors(errors) {
- this.signupErrors = errors;
- }
- @action setActivationError(error) {
- this.activationError = error;
- }
- @action setLoginError = error => {
- this.loginError = error;
- };
- @action clear() {
- this.signupSuccessful = false;
- this.signupErrors = null;
- this.activationError = null;
- this.loginError = null;
- }
- }
- export default new Auth();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement