corrigir erro de criar conta
This commit is contained in:
@@ -14,9 +14,9 @@ type State = {
|
||||
};
|
||||
|
||||
type AppContextValue = State & {
|
||||
login: (email: string, password: string) => boolean;
|
||||
login: (email: string, password: string) => User | null;
|
||||
logout: () => void;
|
||||
register: (payload: Omit<User, 'id' | 'shopId'> & { shopName?: string }) => boolean;
|
||||
register: (payload: Omit<User, 'id' | 'shopId'> & { shopName?: string }) => User | null;
|
||||
addToCart: (item: CartItem) => void;
|
||||
removeFromCart: (refId: string) => void;
|
||||
clearCart: () => void;
|
||||
@@ -74,16 +74,16 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
const found = state.users.find((u) => u.email === email && u.password === password);
|
||||
if (found) {
|
||||
setState((s) => ({ ...s, user: found }));
|
||||
return true;
|
||||
return found;
|
||||
}
|
||||
return false;
|
||||
return null;
|
||||
};
|
||||
|
||||
const logout = () => setState((s) => ({ ...s, user: undefined }));
|
||||
|
||||
const register: AppContextValue['register'] = ({ shopName, ...payload }) => {
|
||||
const exists = state.users.some((u) => u.email === payload.email);
|
||||
if (exists) return false;
|
||||
if (exists) return null;
|
||||
|
||||
if (payload.role === 'barbearia') {
|
||||
const shopId = nanoid();
|
||||
@@ -103,7 +103,7 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
users: [...s.users, user],
|
||||
shops: [...s.shops, shop],
|
||||
}));
|
||||
return true;
|
||||
return user;
|
||||
}
|
||||
|
||||
const user: User = { ...payload, id: nanoid(), role: 'cliente' };
|
||||
@@ -112,7 +112,7 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
user,
|
||||
users: [...s.users, user],
|
||||
}));
|
||||
return true;
|
||||
return user;
|
||||
};
|
||||
|
||||
const addToCart: AppContextValue['addToCart'] = (item) => {
|
||||
@@ -320,4 +320,3 @@ export const useApp = () => {
|
||||
return ctx;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -15,12 +15,13 @@ export default function AuthLogin() {
|
||||
|
||||
const handleSubmit = () => {
|
||||
setError('');
|
||||
const ok = login(email, password);
|
||||
if (!ok) {
|
||||
const user = login(email, password);
|
||||
if (!user) {
|
||||
setError('Credenciais inválidas');
|
||||
Alert.alert('Erro', 'Credenciais inválidas');
|
||||
} else {
|
||||
navigation.navigate('Explore' as never);
|
||||
const target = user.role === 'barbearia' ? 'Dashboard' : 'Explore';
|
||||
navigation.navigate(target as never);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -145,4 +146,3 @@ const styles = StyleSheet.create({
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -18,12 +18,13 @@ export default function AuthRegister() {
|
||||
|
||||
const handleSubmit = () => {
|
||||
setError('');
|
||||
const ok = register({ name, email, password, role, shopName });
|
||||
if (!ok) {
|
||||
const user = register({ name, email, password, role, shopName });
|
||||
if (!user) {
|
||||
setError('Email já registado');
|
||||
Alert.alert('Erro', 'Email já registado');
|
||||
} else {
|
||||
navigation.navigate('Explore' as never);
|
||||
const target = user.role === 'barbearia' ? 'Dashboard' : 'Explore';
|
||||
navigation.navigate(target as never);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -179,4 +180,3 @@ const styles = StyleSheet.create({
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user