Back

Failing to create session (Angular) on vercel.

  • 0
  • Auth
  • Web
  • Cloud
uLuvBobs1337
27 Apr, 2024, 17:46

On local its works perfectly fine, but in vercel where i deployed my front its failing to create a session This is my code:

TypeScript
export class AuthService {
  private readonly SESSION_KEY = 'user_session';
  private client: Client;
  private account: Account;

  private isAuthenticatedSubject: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
  public isAuthenticated$: Observable<boolean> = this.isAuthenticatedSubject.asObservable();

  constructor() {
    this.client = new Client().setEndpoint('https://cloud.appwrite.io/v1');
    this.client.setProject("PROJECT_ID");
    this.account = new Account(this.client);

  }

  async login(email: string, password: string): Promise<void> {
    try {
      //setting email session
      await this.account.createEmailSession(email, password);
      // const privatejwt : Models.Jwt = await this.account.createJWT();
      // console.log(privatejwt);
      // //settign json web token
      // await this.client.setJWT(privatejwt.jwt);
      const sessionData = await this.account.get();
      this.isAuthenticatedSubject.next(true); // Emit true when user is logged in
      localStorage.setItem(this.SESSION_KEY, JSON.stringify(sessionData));
      // localStorage.setItem('appwrite_jwt', privatejwt.jwt);
    } catch (error) {
      console.error('Login error:', error);
      throw new Error('Failed to login. Please check your credentials and try again.');
    }
  }
TL;DR
Developers are facing an issue with creating a session in Angular on Vercel. The code works fine locally but fails on Vercel. They are questioning whether to use `createEmailSession` directly or if a cloud function is needed. The issue may be related to the way authentication is handled for production. A solution was not provided in the thread.
uLuvBobs1337
27 Apr, 2024, 17:46
Steven
27 Apr, 2024, 17:48

What's the console logs show in your browser dev tools?

uLuvBobs1337
27 Apr, 2024, 17:51

this is the console log

uLuvBobs1337
27 Apr, 2024, 17:52

maybe the way i use the auth is wrong for production

uLuvBobs1337
27 Apr, 2024, 17:57

Cant i use emailPassword session diractly from the cloud to appwrite ? or that i need a cloudfunction that will handle this ?

Steven
27 Apr, 2024, 17:59

Screenshot please

Steven
27 Apr, 2024, 17:59

No you don't need a function

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