{"id":25286268,"url":"https://github.com/thecomputerm/tauri-logto","last_synced_at":"2025-10-14T11:08:59.599Z","repository":{"id":275113382,"uuid":"925099506","full_name":"TheComputerM/tauri-logto","owner":"TheComputerM","description":null,"archived":false,"fork":false,"pushed_at":"2025-02-01T19:55:43.000Z","size":260,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-06T15:43:20.808Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TheComputerM.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-31T08:19:01.000Z","updated_at":"2025-02-01T19:55:46.000Z","dependencies_parsed_at":null,"dependency_job_id":"ecca2247-4388-4b14-90ef-1e3e23f274b6","html_url":"https://github.com/TheComputerM/tauri-logto","commit_stats":null,"previous_names":["thecomputerm/tauri-logto"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheComputerM%2Ftauri-logto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheComputerM%2Ftauri-logto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheComputerM%2Ftauri-logto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheComputerM%2Ftauri-logto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TheComputerM","download_url":"https://codeload.github.com/TheComputerM/tauri-logto/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheComputerM%2Ftauri-logto/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259043762,"owners_count":22797159,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-12T21:35:20.637Z","updated_at":"2025-10-14T11:08:54.558Z","avatar_url":"https://github.com/TheComputerM.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Tauri + OAuth2 (Logto)\n\nThis repository has a tauri app that works as both native and a plain vanilla web build. Inspect its codebase to learn more.\n\nThere are two main ways to get OAuth2 working on Tauri.\n\n1) Using Deeplinks, more mobile app like\n2) Using a separate server to catch the redirect url, using the oauth-plugin\n\n## Using Deeplinks\n\n### Step 1: Add plugins and dependencies\n\n- [tauri deep linking plugin](https://v2.tauri.app/plugin/deep-linking/)\n- [tauri single instacne plugin](https://v2.tauri.app/plugin/single-instance/) - recommended\n- [tauri opener plugin](https://v2.tauri.app/plugin/opener/)\n- [tauri oauth plugin](https://github.com/FabianLars/tauri-plugin-oauth)\n- [@logto/browser](https://www.npmjs.com/package/@logto/browser)\n\nAfter adding these, make sure you configure these for the platforms of your choosing.\n\nFor this example, I am assuming `tauri-logto://` as a deep link to the app. I recommend dynamically registering the deep link on desktop platforms.\n\n----\n\n### Step 2: Enable withGlobalTauri (optional)\n\nI recommend this step **IF** you want your app to run on the web browser along with being a native app.\n\n```json\n// tauri.conf.json\n{\n  \"app\": {\n    \"withGlobalTauri\": true,\n  }\n}\n```\n\n```js\nexport const isTauri = \"__TAURI__\" in window;\n```\n\n----\n\n### Step 3: Create a Logto native application\n\n**Redirect URIs:**\n\n1. tauri-logto://callback\n2. http://localhost:1420/callback (for the plain web build)\n\n**Post sign-out redirect URIs:**\n\n1. tauri-logto://callback\n2. http://localhost:1420/ (for the plain web build)\n\n----\n\n### Step 4: Patch Logto's redirect\n\nMake sure to use the opener plugin to redirect instead of the tauri webview.\n\n```ts\nexport const logtoClient = new LogtoClient({\n\tendpoint: import.meta.env.VITE_LOGTO_ENDPOINT,\n\tappId: import.meta.env.VITE_LOGTO_APP_ID,\n});\n\nif (isTauri) {\n\tlogtoClient.adapter.navigate = (url) =\u003e openUrl(url);\n}\n```\n\n----\n\n### Step 5: Handle Login\n\nThe fun part. \n\n```rust\nlet mut builder = tauri::Builder::default();\n\n#[cfg(desktop)]\n{\n  builder = builder.plugin(tauri_plugin_single_instance::init(|app, argv, _cwd| {\n    if argv.len() \u003e 1 \u0026\u0026 argv[1].starts_with(\"tauri-logto://callback\") {\n      let redirect_uri = argv[1].clone();\n      app.emit(\"redirect_uri\", redirect_uri)\n        .expect(\"failed to emit redirect_uri\");\n    }\n  }));\n}\n\nbuilder\n  .plugin(tauri_plugin_opener::init())\n  .plugin(tauri_plugin_deep_link::init())\n  //...\n```\n\n```ts\nasync function handleTauriLogin() {\n  await logtoClient.signIn(\"tauri-logto://callback\");\n  const stopListener = await listen\u003cstring\u003e(\n    \"redirect_uri\",\n    async (event) =\u003e {\n      await logtoClient.handleSignInCallback(event.payload);\n      if (await logtoClient.isAuthenticated()) {\n        stopListener?.();\n        navigate(\"/app\");\n      }\n    },\n  );\n}\n```\n\n## Using Tauri Oauth Plugin\n\n### Step 1: Add plugins and dependencies\n\n- [tauri opener plugin](https://v2.tauri.app/plugin/opener/)\n- [tauri oauth plugin](https://github.com/FabianLars/tauri-plugin-oauth)\n- [@logto/browser](https://www.npmjs.com/package/@logto/browser)\n\n----\n\n### Step 2: Create a Logto native application\n\n**Redirect URIs:**\n\n1. http://localhost (for the server created by the plugin)\n\n**CORS allowed origins:**\n\n1. http://localhost:1420 (for app during dev)\n2. tauri://localhost (for app during production)\n\n\nWe need `tauri://localhost` to cors allowed origins because that is the custom protocol used in production, if you want to change that use the [tauri localhost plugin](https://v2.tauri.app/plugin/localhost/).\n\n----\n\n### Step 3: Patch Logto's redirect\n\nMake sure to use the opener plugin to redirect instead of the tauri webview.\n\n```ts\nexport const logtoClient = new LogtoClient({\n\tendpoint: import.meta.env.VITE_LOGTO_ENDPOINT,\n\tappId: import.meta.env.VITE_LOGTO_APP_ID,\n});\n\nif (isTauri) {\n\tlogtoClient.adapter.navigate = (url) =\u003e openUrl(url);\n}\n```\n\n### Step 4: Handle Login\n\n```ts\nimport { onUrl, start } from \"@fabianlars/tauri-plugin-oauth\";\nimport { logtoClient } from \"../lib/logto\";\n\n// when the login button is clicked\nasync function handleSignin() {\n  const port = await start();\n  await logtoClient.signIn(`http://localhost:${port}`);\n\n  await onUrl(async (url) =\u003e {\n    await logtoClient.handleSignInCallback(url);\n    if (await logtoClient.isAuthenticated()) {\n      alert(\"Authentication successful!\");\n      // stop callback server\n    } else {\n      alert(\"Authentication failed!\");\n    }\n  });\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecomputerm%2Ftauri-logto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthecomputerm%2Ftauri-logto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecomputerm%2Ftauri-logto/lists"}