@ -4,57 +4,60 @@ import axios from "axios";
import { json } from "express" ;
import { json } from "express" ;
import { useEffect } from "react" ;
import { useEffect } from "react" ;
import { API_URL } from "../../fladConfig" ;
import { API_URL } from "../../fladConfig" ;
import { Credentials , CredentialsRegister , restoreToken , setLoginState , UserLogout , userChangeMode , userSignUp } from "../actions/userActions" ;
import { Credentials , CredentialsRegister , restoreToken , setLoginState , UserLogout , userChangeMode , userSignUp , ChangeErrorLogin , ChangeErrorSignup } from "../actions/userActions" ;
import * as SecureStore from 'expo-secure-store' ;
import * as SecureStore from 'expo-secure-store' ;
import { User } from "../../Model/User" ;
import { User } from "../../Model/User" ;
import { UserFactory } from "../../Model/factory/UserFactory" ;
import { UserFactory } from "../../Model/factory/UserFactory" ;
import * as ImagePicker from 'expo-image-picker' ;
const key = 'userToken' ;
const key = 'userToken' ;
export const registerUser = ( resgisterCredential : CredentialsRegister ) = > {
export const registerUser = ( resgisterCredential : CredentialsRegister ) = > {
//@ts-ignore
//@ts-ignore
return async dispatch = > {
return async dispatch = > {
try {
try {
console . log ( resgisterCredential ) ;
console . log ( resgisterCredential ) ;
const config = {
const config = {
headers : {
headers : {
'Content-Type' : 'application/json' ,
'Content-Type' : 'application/json' ,
} ,
} ,
}
}
const resp = await axios . post (
const resp = await axios . post (
` ${ API_URL } /api/users/register ` ,
` ${ API_URL } /api/users/register ` ,
resgisterCredential ,
resgisterCredential ,
config
config
)
)
if ( resp . data . token ) {
if ( resp . data . token ) {
console . log ( resp . data . token ) ;
console . log ( resp . data . token ) ;
const token = resp . data . token ;
const token = resp . data . token ;
// await SecureStore.setItemAsync(key, token);
// await SecureStore.setItemAsync(key, token);
const headers = {
const headers = {
'Authorization' : 'Bearer ' + token } ;
'Authorization' : 'Bearer ' + token
} ;
const user = await axios . get (
const user = await axios . get (
"https://flad-api-production.up.railway.app/api/users" ,
"https://flad-api-production.up.railway.app/api/users" ,
{ headers }
{ headers }
)
)
dispatch ( userSignUp ( UserFactory . JsonToModel ( user . data ) ) ) ; // our action is called here
dispatch ( userSignUp ( UserFactory . JsonToModel ( user . data ) ) ) ; // our action is called here
// console.log(user.data);
// console.log(user.data);
// dispatch(setLoginState(user.data) ); // our action is called here
// dispatch(setLoginState(user.data) ); // our action is called here
} else {
} else {
console . log ( 'Login Failed' , 'Username or Password is incorrect' ) ;
console . log ( 'Login Failed' , 'Username or Password is incorrect' ) ;
}
}
// if (resp.data.msg === 'success') { // response success checking logic could differ
// if (resp.data.msg === 'success') { // response success checking logic could differ
// await SecureStore.setItemAsync(key, resp.data.token);
// await SecureStore.setItemAsync(key, resp.data.token);
// dispatch(setLoginState(resp.data.user) ); // our action is called here
// dispatch(setLoginState(resp.data.user) ); // our action is called here
// } else {
// } else {
// console.log('Login Failed', 'Username or Password is incorrect');
// console.log('Login Failed', 'Username or Password is incorrect');
// }
// }
} catch ( error ) {
} catch ( error ) {
console . log ( 'Error---------' , error ) ;
console . log ( 'Error---------' , error ) ;
dispatch ( ChangeErrorSignup ( ) )
}
}
}
}
}
}
@ -95,12 +98,14 @@ export const userLogin = (loginCredential: Credentials) => {
)
)
// dispatch(setLoginState(resp.data.user) ); // our action is called here
// dispatch(setLoginState(resp.data.user) ); // our action is called here
console . log ( user . data ) ;
console . log ( user . data ) ;
dispatch ( setLoginState ( user . data ) ) ; // our action is called here
dispatch ( setLoginState ( user . data ) ) ; // our action is called here
} else {
} else {
console . log ( 'Login Failed' , 'Username or Password is incorrect' ) ;
console . log ( 'Login Failed' , 'Username or Password is incorrect' ) ;
}
}
} catch ( error ) {
} catch ( error ) {
dispatch ( ChangeErrorLogin ( ) )
console . log ( 'Error---------' , error ) ;
console . log ( 'Error---------' , error ) ;
}
}
}
}
@ -146,14 +151,21 @@ export const DeleteToken = () => {
}
}
}
}
export const ChangeMode = ( ) = > {
export const ChangeMode = ( value : boolean ) = > {
//@ts-ignore
//@ts-ignore
return async dispatch = > {
return async dispatch = > {
dispatch ( userChangeMode ( ) ) ;
dispatch ( userChangeMode ( value ) ) ;
await SecureStore . deleteItemAsync ( key ) ;
}
}
}
}
export const ChangeImageUserCurrent = ( value : ImagePicker ) = > {
//@ts-ignore
return async dispatch = > {
dispatch ( userChangeImage ( value ) ) ;
}
}
// const logIn = (email, password) => {
// const logIn = (email, password) => {
// const action = (dispatch) => {
// const action = (dispatch) => {
// if (email === user.email && password === user.password) {
// if (email === user.email && password === user.password) {