
auth.js:
TypeScript
import conf from '../conf/conf.js';
import { Client, Account, ID } from "appwrite";
export class AuthService {
client = new Client();
account;
constructor() {
this.client
.setEndpoint(conf.appwriteUrl)
.setProject(conf.appwriteProjectId);
this.account = new Account(this.client);
}
async createAccount({ email, password, name }) {
try {
const userAccount = await this.account.create(ID.unique(), email, password, name);
if (userAccount) {
return this.login({ email, password });
} else {
return userAccount;
}
} catch (error) {
throw error;
}
}
async login({ email, password }) {
try {
return await this.account.createEmailSession(email, password);
} catch (error) {
throw error;
}
}
async getCurrentUser() {
try {
return await this.account.get();
} catch (error) {
console.log("Appwrite serive :: getCurrentUser :: error", error);
}
return null;
}
async logout() {
try {
await this.account.deleteSessions();
} catch (error) {
console.log("Appwrite serive :: logout :: error", error);
}
}
}
const authService = new AuthService();
export default authService
.env:
TypeScript
VITE_APPWRITE_URL=https://cloud.appwrite.io/v1
VITE_APPWRITE_PROJECT_ID="6**************4"
VITE_APPWRITE_DATABASE_ID='6*************8'
VITE_APPWRITE_COLLECTION_ID='6***********d'
VITE_APPWRITE_BUCKET_ID='6****************d'
I have attached the screenshot of the conf,js and logs, please help me to fix this issue
TL;DR
Developers are getting a 401 Unauthorized error when trying to access the account details in the Appwrite API. The issue is likely with the authentication process. The provided code shows the AuthService class with methods for creating an account, logging in, getting the current user, and logging out. The .env file contains the necessary environment variables. Check the authentication flow and ensure the credentials are correct.Recommended threads
- [FEATURE] Better usage analytics for app...
Recently, i've gotten **73** emails from appwrite regarding excesive GBHours usage. I've almost hit the limit of 1000 and it is really hard to track down which ...
- Images not showing up --
so i made this movie app - i hosted it successfully using appwrite but the images arent showing up --- https://movie-app-jsm.appwrite.network/ this is the movie...
- Cannot add/update array field
How do I add or update the rows, How exactly to pass properties? (see provided image)
