Back

Doing everything in scratch

  • 0
  • Auth
  • Web
  • Cloud
Zip
8 Jun, 2024, 13:14

So right now, i'm following appwrite's doc in next js. The problem is this:

TL;DR
Ensure the type for `loggedInUser` is set to `<User<Preferences> | null>`. Update code snippet accordingly.
Zip
8 Jun, 2024, 13:14
TypeScript
"use client";
import { useState } from "react";
import {Client, Account, ID, Databases, Permission, Role, Teams} from 'appwrite'

const conf = {
    appwriteUrl: String(process.env.NEXT_PUBLIC_APPWRITE_URL),
    appwriteProjectId: String(process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID),
    appwriteAPIKey: String(process.env.NEXT_PUBLUC_APPWRITE_API_KEY),
}

  

const appwriteClient = new Client()
    .setEndpoint(conf.appwriteUrl)
    .setProject(conf.appwriteProjectId);

export const account = new Account(appwriteClient)
export const databases = new Databases(appwriteClient);


const useAuth = () => {
    const [loggedInUser, setLoggedInUser] = useState(null);
    const [email, setEmail] = useState("");
    const [password, setPassword] = useState("");
    const [name, setName] = useState("");

    const register = async () => {
        try {
            await account.create(ID.unique(), email, password, name);
            return login(email, password);
        } catch (error:any) {
            console.log("register error" + error)
        }
        
    };

    const login = async (email: string, password: string) => {
        try {
            return await account.createEmailPasswordSession(email, password);
            const user = await account.get()
            setLoggedInUser(user);
        } catch (error:any) {
            console.log("login error: " + error)
        }
        
    };

    const currentuser = async () => {
        try {
            const user = await account.get()
            return user
        } catch (error:any) {
            console.log("currentUser error: " + error)
        }
    };


    const logout = async () => {
        await account.deleteSession("current");
        setLoggedInUser(null);
    };

    return { 
        register,
        login, 
        logout,
        currentuser,
        email, 
        setEmail, 
        password, 
        setPassword, 
        name, 
        setName, 
        loggedInUser 
    };
};

export default useAuth;
Zip
8 Jun, 2024, 13:14
Zip
8 Jun, 2024, 13:14

i followed the doc

Ryan
8 Jun, 2024, 13:18

The type being used for loggedInUser is null here: const [loggedInUser, setLoggedInUser] = useState(null);

Try doing this instead:

TypeScript
const [loggedInUser, setLoggedInUser] = useState<User<Preferences> | null>(null);
Zip
8 Jun, 2024, 13:37

User & Preferences gives an error, tried searching in the web but its showing that i should import "user" from appwrite but i dont think theres a "user" in the docs

Zip
8 Jun, 2024, 13:44

Doing everything in scratch

Ryan
8 Jun, 2024, 13:49

Try setting the type to <Models.User<Preferences> | null> instead, I think that might how it's being exported from the Appwrite package

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more