#Capacitorjs Deep Link configured, in ios/android, How to deal with OAuth2 ?

65 messages · Page 1 of 1 (latest)

main heart
#

Before i start , i use only web-sdk with capacitorjs. It builds, PWA, iOS and Android clients.
createEmailSession works fine with custom domain in PWA and also iOS and android clients. It sets cookies and session works without any error. -
--demo video 1--

createOAuth2Session works fine with only custom domain in PWA. Not in iOS/android clients.
Success link is going to app by deep linking configuration. (shown in demo video).

But the problem is in ios app, session is not started (cookies are not set) thats why i cant move further.
In the same time, it sets cookies into PWA, when i try to open PWA in browser in simulator, user logged in, but user not logged in ios app.
--demo video 2--

How can i handle or transfer/copy cookies from web app to mobile app ? Is there any way to transfer it? Any idea?

Related thread: 1145408157343039498

#

Capacitorjs Deep Link configured, in ios/android, OAuth2 cookies are not extracted!

main heart
elder pivot
main heart
#

Actually appwrite response after createEmailSession / createOauthSession sets automatically in PWA. I did not do anything extra.

#

However deeplinking is just split url and navigate..

  async ngOnInit() {
    // Init Deep Link
    this.initDeepLink();
  }
  // Deep Link
  initDeepLink() {
    App.addListener('appUrlOpen', (event: URLOpenListenerEvent) => {
      this.zone.run(() => {
        const slug = event.url.split('app.languagexchange.net').pop();
        if (slug) {
          this.router.navigateByUrl(slug);
        }
      });
    });
  }
#

Copilot feedback.

elder pivot
#

So either you need to open the oauth url in the capacitor app context or you need to figure out how to set the cookie

main heart
#

In this example it handles incoming cookies in AppDelegate.swift
Does it work with only apple-sdk yeah ?

image ref: appwrite-docs

main heart
#

Ok, found a solution and it works! If you anyone encounter same/similar challenge, hope this thread gonna be helpful for the community.

Before i start i was exhausted about this issue how to solve it, it looks like a basic oauth2 integration but it made me crazy several days.

#

So i was asking the question to copilot, i got this answer and enlightened my mind. It was a common challenge.

#

However appwrite uses only this following headers

#

Final question was that "Which header is better approach to handle with this situation ? What do you think about that ?" to AI and it said JWT!

#

Cool, i just create a session create jwt from browser session and pass through to mobile app page by using deeplink and then triggered set jwt

client: Client = new Client();

constructor(private http: HttpClient) {
  this.init();
}

init(): void {
  this.client
    .setEndpoint(environment.appwrite.APP_ENDPOINT) // Your API Endpoint
    .setProject(environment.appwrite.APP_PROJECT); // Your project ID
}

setJWT(jwt: string): void {
  this.client.setJWT(jwt);
}
#

Capacitorjs Deep Link configured, in ios/android, How to deal with OAuth2 ?

cosmic tartan
main heart
main heart
elder pivot
main heart
#

setting cookie is the most easiest thing. but i didn’t get your point. How can i extract that cookies? I tried lots of different scenarios from client and server side as well

elder pivot
#

To get the cookie, you'll redirect back to your app at this path:

/auth/oauth2/success
main heart
#

only this url has cookie but browser triggers this request. So after that request executed then it redirects to my success URL in location part which there is no cookies in headers. I can listen success url but not the first request.

elder pivot
main heart
#
elder pivot
main heart
elder pivot
main heart
elder pivot
main heart
elder pivot
main heart
#

but where did i know this url ? any link from the Docs or somewhere?

main heart
#

i really thank you to solve that one, i am really appreciate! It consumed pretty much of my time !!

#

Happy New Year 🎄

agile vigil
#

hi there @main heart happy you figured out your issue there....

What method did you use to create the session? Also, is this only possible if you use deeplinking and set your appwrite api url to your custom domain?

main heart
main heart
agile vigil
agile vigil
#

Oh now I understand what you mean @main heart! Worked for me as well I got back the key and secret at /auth/oauth2/success. Thankfully less than a week thanks to you folks

cosmic tartan
agile vigil
main heart
#

great question

#

let me check but i didnt use capacitor.storage in the particular one as i remember

agile vigil
#

trying this to test

App.addListener('appUrlOpen', async (event: URLOpenListenerEvent) => {
        const slug = event.url.split('/auth/oauth2/success').pop();
        if (slug) {
          
          const params = new URLSearchParams(slug);
          const key = params.get('key')
          const secret = params.get('secret')
          if (!secret || !key) return false
          await CapacitorCookies.setCookie({
            url: import.meta.env.VITE_APPWRITE_ENDPOINT,
            key,
            value: secret
          })
          await CapacitorCookies.setCookie({
            url: 'localhost',
            key,
            value: secret
          })
........
#

thank you so much for checking @main heart

main heart
#
  initDeepLink() {
    App.addListener('appUrlOpen', (event: URLOpenListenerEvent) => {
      this.zone.run(() => {
        console.log('appUrlOpen', event);
        const slug = event.url.split(environment.url.HOMEPAGE_URL).pop();
        console.log('slug: ', slug);
        // TODO; First redirect page to check auth status (auth/oauth2/..) and redirect to slug
        this.router.navigateByUrl(slug);
      });
    });
  }
}

ref

#

here i just redirect the url , no more

#

i set cookie in the related page.ts: ref

  async initValues() {
    // Get Selectors
    this.isLoggedIn$ = this.store.pipe(select(isLoggedInSelector));
    this.isLoading$ = this.store.pipe(select(isLoadingSelector));

    // Set Params
    const params = this.route.snapshot.queryParamMap;

    const cookieFallback = {};
    const key = params.get('key');
    const secret = params.get('secret');

    if (key) {
      cookieFallback[key] = secret;
    }

    const cookieValue = JSON.stringify(cookieFallback);

    if (
      cookieFallback &&
      Object.keys(cookieFallback).length !== 0 &&
      Capacitor.getPlatform() !== 'web'
    ) {
      localStorage.setItem('cookieFallback', cookieValue);
    }

    this.store.dispatch(isLoggedInAction());
  }
agile vigil
#

aaaah very cool. just local storage!

#

and then for making subsequent appwrite requests (ie account.getSession('current') ).... how did you set the session?

main heart
#

yea i did it like that but i think you can also use it Capacitor.Storage but didnt test it yet. it works like that but i will take a look here later on because of security reasons later on.

Storing small to large amounts of data in Capacitor

main heart
agile vigil
#

aaah brilliant thank you. can confirm now that CapacitorCookies not working out of the box...for me at least

#

thanks again

main heart
#

possibly 🙂 np

agile vigil
#

Confirmed setting localStorage key works - not so much with the Capacitor.Preferences....