diff --git a/app/mobile/src/access/login/Login.jsx b/app/mobile/src/access/login/Login.jsx index 8e2fe543..f67715ee 100644 --- a/app/mobile/src/access/login/Login.jsx +++ b/app/mobile/src/access/login/Login.jsx @@ -1,4 +1,4 @@ -import { Text, TextInput, View, TouchableOpacity } from 'react-native'; +import { ActivityIndicator, Alert, Text, TextInput, View, TouchableOpacity } from 'react-native'; import { styles } from './Login.styled'; import Ionicons from '@expo/vector-icons/AntDesign'; import { useLogin } from './useLogin.hook'; @@ -7,6 +7,18 @@ export function Login() { const { state, actions } = useLogin(); + const login = async () => { + try { + await actions.login(); + } + catch (err) { + Alert.alert( + "Login Failed", + "Please check your login and password.", + ); + } + } + return ( @@ -26,17 +38,34 @@ export function Login() { autoCapitalize="none" /> - - - - - - - + { state.showPassword && ( + + + + + + + + )} + { !state.showPassword && ( + + + + + + + + )} { state.enabled && ( - - Login + + { state.busy && ( + + )} + { !state.busy && ( + Login + )} )} { !state.enabled && ( @@ -44,16 +73,9 @@ export function Login() { Login )} - { state.createable && ( - - Create Account - - )} - { !state.createable && ( - - Create Account - - )} + + Create Account + diff --git a/app/mobile/src/access/login/useLogin.hook.js b/app/mobile/src/access/login/useLogin.hook.js index 48b4ff16..f7cbfa49 100644 --- a/app/mobile/src/access/login/useLogin.hook.js +++ b/app/mobile/src/access/login/useLogin.hook.js @@ -1,16 +1,19 @@ -import { useState, useEffect } from 'react'; +import { useState, useEffect, useContext } from 'react'; import { useWindowDimensions } from 'react-native'; import { useNavigate } from 'react-router-dom'; +import { AppContext } from 'context/AppContext'; export function useLogin() { const navigate = useNavigate(); + const app = useContext(AppContext); const [state, setState] = useState({ - createable: false, + busy: false, enabled: false, login: null, password: null, + showPassword: false, }); const updateState = (value) => { @@ -36,6 +39,30 @@ export function useLogin() { setPassword: (password) => { updateState({ password }); }, + create: () => { + navigate('/create'); + }, + showPassword: () => { + updateState({ showPassword: true }); + }, + hidePassword: () => { + updateState({ showPassword: false }); + }, + login: async () => { + if (!state.busy) { + updateState({ busy: true }); + try { + await app.actions.login(state.login, state.password); + navigate('/'); + } + catch (err) { + console.log(err); + updateState({ busy: false, showAlert: true }); + throw new Error('login failed'); + } + updateState({ busy: false }); + } + } }; return { state, actions }; diff --git a/app/mobile/src/api/setLogin.js b/app/mobile/src/api/setLogin.js index ab324d07..074f60c8 100644 --- a/app/mobile/src/api/setLogin.js +++ b/app/mobile/src/api/setLogin.js @@ -1,11 +1,11 @@ import { checkResponse, fetchWithTimeout } from './fetchUtil'; import base64 from 'react-native-base64' -export async function setLogin(username, password) { +export async function setLogin(username, server, password) { let headers = new Headers() headers.append('Authorization', 'Basic ' + base64.encode(username + ":" + password)); - let app = { Name: "indicom", Description: "decentralized communication" } - let login = await fetchWithTimeout('/account/apps', { method: 'POST', body: JSON.stringify(app), headers: headers }) + let app = { Name: "topics", Description: "decentralized communication" } + let login = await fetchWithTimeout(`https://${server}/account/apps`, { method: 'POST', body: JSON.stringify(app), headers: headers }) checkResponse(login) return await login.json() } diff --git a/app/mobile/src/context/useAppContext.hook.js b/app/mobile/src/context/useAppContext.hook.js index 03deaba0..f3611bc6 100644 --- a/app/mobile/src/context/useAppContext.hook.js +++ b/app/mobile/src/context/useAppContext.hook.js @@ -51,7 +51,9 @@ export function useAppContext() { } const appLogin = async (username, password) => { + const acc = username.split('@'); let access = await setLogin(acc[0], acc[1], password) +console.log(access); setWebsocket(acc[1], access.appToken) updateState({ session: true, token: access.appToken, server: acc[1] }); // store