
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
- How can I use react-native-appwrite in a...
I'm building an app using React Native CLI (not Expo), and I want to add Google Sign-In authentication. However, when I try to use react-native-appwrite, it thr...
- Appwrite custom domain verification fail...
So I've left enough time for the records to propagate and I've tried adding in a subdomain for the appwrite endpoint so as not to cause a clash with two CNAME r...
- Error: Invalid `userId` param: Parameter...
