File size: 4,308 Bytes
be02369
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import React, { createContext, useState, useEffect, useCallback } from 'react';
import type { UserProfile } from '../types';
import { KOFI_LOGIN_NAME, KOFI_SECRET_PASSWORD } from '../config';

// Keys for localStorage
const CUSTOM_NAME_KEY = 'yuki-app-login-name';
const CUSTOM_SECRET_KEY = 'yuki-app-login-secret';
const SESSION_KEY = 'yuki-user-session';

interface IAuthContext {
    user: UserProfile | null;
    isLoading: boolean;
    login: (name: string, secret: string) => boolean;
    logout: () => void;
    updateCredentials: (currentSecret: string, newName: string, newSecret: string) => { success: boolean; message: string };
}

export const AuthContext = createContext<IAuthContext>({
    user: null,
    isLoading: true,
    login: () => false,
    logout: () => {},
    updateCredentials: () => ({ success: false, message: 'Not implemented' }),
});

const createAuthenticatedUser = (name: string): UserProfile => ({
    id: `user-${name}`,
    name: "Bonsai Master",
    email: '', // Not needed for this auth model
    picture: '', // Not needed for this auth model
});


export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
    const [user, setUser] = useState<UserProfile | null>(null);
    const [isLoading, setIsLoading] = useState(true);
    const storageKey = 'yuki-user-session'; // Keep existing name for session for backward compatibility

    useEffect(() => {
        try {
            const session = window.localStorage.getItem(storageKey);
            if (session) {
                const sessionUser = JSON.parse(session);
                setUser(sessionUser);
            }
        } catch (error) {
            console.error("Failed to parse user session:", error);
            window.localStorage.removeItem(storageKey);
        } finally {
            setIsLoading(false);
        }
    }, [storageKey]);

    const login = useCallback((name: string, secret: string): boolean => {
        const customName = window.localStorage.getItem(CUSTOM_NAME_KEY);
        const customSecret = window.localStorage.getItem(CUSTOM_SECRET_KEY);

        const effectiveName = customName || KOFI_LOGIN_NAME;
        const effectiveSecret = customSecret || KOFI_SECRET_PASSWORD;

        if (name === effectiveName && secret === effectiveSecret) {
            const authenticatedUser = createAuthenticatedUser(name);
            try {
                window.localStorage.setItem(storageKey, JSON.stringify(authenticatedUser));
                setUser(authenticatedUser);
                return true;
            } catch (error) {
                console.error("Failed to save user session:", error);
                return false;
            }
        }
        return false;
    }, [storageKey]);

    const logout = useCallback(() => {
        try {
            window.localStorage.removeItem(storageKey);
            setUser(null);
        } catch (error) {
            console.error("Failed to clear user session:", error);
        }
    }, [storageKey]);
    
    const updateCredentials = useCallback((currentSecret: string, newName: string, newSecret: string): { success: boolean; message: string } => {
        const customSecret = window.localStorage.getItem(CUSTOM_SECRET_KEY);
        const effectiveSecret = customSecret || KOFI_SECRET_PASSWORD;

        if (currentSecret !== effectiveSecret) {
            return { success: false, message: "Current secret password is incorrect." };
        }
        
        if (!newName.trim() || !newSecret.trim()) {
            return { success: false, message: "New login name and secret cannot be empty." };
        }

        try {
            window.localStorage.setItem(CUSTOM_NAME_KEY, newName);
            window.localStorage.setItem(CUSTOM_SECRET_KEY, newSecret);
            return { success: true, message: "Credentials updated successfully! You will need to use these next time you log in." };
        } catch (error) {
            console.error("Failed to update credentials:", error);
            return { success: false, message: "Failed to save new credentials due to a storage error." };
        }
    }, []);

    return (
        <AuthContext.Provider value={{ user, isLoading, login, logout, updateCredentials }}>
            {children}
        </AuthContext.Provider>
    );
};