
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
- Collection Permission issue
I am facing issue in my Pro account. "Add" button is disabled while adding permission in DB collection settings.
- SSO google apple not working anymore
We use Apple and Google sso in our react native app. Everything worked fine until we noticed today that we see general argument error. We did not change anythi...
- Opened my website after long time and Ba...
I built a website around a year back and and used appwrite for making the backend. At that time the website was working fine but now when i open it the images a...
