├── .env.example ├── .eslintrc.cjs ├── .github └── workflows │ └── ci.yml ├── .gitignore ├── .npmrc ├── .prettierrc.cjs ├── .vscode ├── extensions.json └── settings.json ├── README.md ├── apps ├── expo │ ├── .expo-shared │ │ └── assets.json │ ├── app.json │ ├── assets │ │ ├── adaptive-icon.png │ │ ├── favicon.png │ │ ├── icon.png │ │ └── splash.png │ ├── babel.config.js │ ├── index.ts │ ├── metro.config.js │ ├── package.json │ ├── src │ │ ├── _app.tsx │ │ ├── components │ │ │ └── SignInWithOAuth.tsx │ │ ├── constants.ts │ │ ├── screens │ │ │ ├── home.tsx │ │ │ └── signin.tsx │ │ ├── types │ │ │ └── nativewind.d.ts │ │ └── utils │ │ │ ├── cache.ts │ │ │ └── trpc.tsx │ ├── tailwind.config.cjs │ └── tsconfig.json └── nextjs │ ├── .eslintrc.cjs │ ├── README.md │ ├── next-env.d.ts │ ├── next.config.mjs │ ├── package.json │ ├── postcss.config.cjs │ ├── public │ └── favicon.ico │ ├── src │ ├── env │ │ ├── client.mjs │ │ ├── schema.mjs │ │ └── server.mjs │ ├── middleware.ts │ ├── pages │ │ ├── _app.tsx │ │ ├── api │ │ │ └── trpc │ │ │ │ └── [trpc].ts │ │ ├── index.tsx │ │ ├── sign-in │ │ │ └── [[...index]].tsx │ │ └── sign-up │ │ │ └── [[...index]].tsx │ ├── styles │ │ └── globals.css │ └── utils │ │ └── trpc.ts │ ├── tailwind.config.cjs │ └── tsconfig.json ├── package.json ├── packages ├── api │ ├── index.ts │ ├── package.json │ ├── src │ │ ├── context.ts │ │ ├── router │ │ │ ├── auth.ts │ │ │ ├── index.ts │ │ │ └── post.ts │ │ └── trpc.ts │ ├── transformer.ts │ └── tsconfig.json ├── config │ └── tailwind │ │ ├── index.js │ │ ├── package.json │ │ └── postcss.js └── db │ ├── index.ts │ ├── package.json │ ├── prisma │ ├── db.sqlite │ └── schema.prisma │ └── tsconfig.json ├── patches └── react-native@0.70.5.patch ├── pnpm-lock.yaml ├── pnpm-workspace.yaml ├── tsconfig.json └── turbo.json /.env.example: -------------------------------------------------------------------------------- 1 | # Since .env is gitignored, you can use .env.example to build a new `.env` file when you clone the repo. 2 | # Keep this file up-to-date when you add new variables to \`.env\`. 3 | 4 | # This file will be committed to version control, so make sure not to have any secrets in it. 5 | # If you are cloning this repo, create a copy of this file named `.env` and populate it with your secrets. 6 | 7 | # We use dotenv to load Prisma from Next.js' .env file 8 | # @see https://www.prisma.io/docs/reference/database-reference/connection-urls 9 | DATABASE_URL=file:./db.sqlite 10 | 11 | # CLERK is used for authentication and authorization in the app 12 | # @see https://dashboard.clerk.dev for your Clerk API keys 13 | # 14 | ### NEW KEYS FOR APPS AFTER 1/18/2023 15 | NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_XXXXXXXXXXXXXXXXXXXX 16 | CLERK_SECRET_KEY=sk_test_XXXXXXXXXXXXXXXXXXXXXXXX 17 | -------------------------------------------------------------------------------- /.eslintrc.cjs: -------------------------------------------------------------------------------- 1 | /** @type {import("eslint").Linter.Config} */ 2 | module.exports = { 3 | root: true, 4 | parser: "@typescript-eslint/parser", 5 | parserOptions: { 6 | tsconfigRootDir: __dirname, 7 | project: [ 8 | "./tsconfig.json", 9 | "./apps/*/tsconfig.json", 10 | "./packages/*/tsconfig.json", 11 | ], 12 | }, 13 | plugins: ["@typescript-eslint"], 14 | extends: ["plugin:@typescript-eslint/recommended"], 15 | }; 16 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | pull_request: 5 | branches: ["*"] 6 | push: 7 | branches: ["main"] 8 | 9 | # You can leverage Vercel Remote Caching with Turbo to speed up your builds 10 | # @link https://turborepo.org/docs/core-concepts/remote-caching#remote-caching-on-vercel-builds 11 | env: 12 | TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }} 13 | TURBO_TEAM: ${{ secrets.TURBO_TEAM }} 14 | NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: ${{secrets.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY}} 15 | CLERK_SECRET_KEY: ${{secrets.CLERK_SECRET_KEY}} 16 | 17 | 18 | jobs: 19 | build-lint: 20 | env: 21 | DATABASE_URL: file:./db.sqlite 22 | runs-on: ubuntu-latest 23 | 24 | steps: 25 | - name: Checkout repo 26 | uses: actions/checkout@v3 27 | 28 | - name: Setup pnpm 29 | uses: pnpm/action-setup@v2.2.4 30 | 31 | - name: Setup Node 18 32 | uses: actions/setup-node@v3 33 | with: 34 | node-version: 18 35 | 36 | - name: Get pnpm store directory 37 | id: pnpm-cache 38 | run: | 39 | echo "pnpm_cache_dir=$(pnpm store path)" >> $GITHUB_OUTPUT 40 | 41 | - name: Setup pnpm cache 42 | uses: actions/cache@v3 43 | with: 44 | path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }} 45 | key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} 46 | restore-keys: | 47 | ${{ runner.os }}-pnpm-store- 48 | 49 | - name: Install deps (with cache) 50 | run: pnpm install 51 | 52 | - name: Next.js cache 53 | uses: actions/cache@v3 54 | with: 55 | path: ${{ github.workspace }}apps/nextjs/.next/cache 56 | key: ${{ runner.os }}-${{ runner.node }}-${{ hashFiles('**/pnpm-lock.yaml') }}-nextjs 57 | 58 | - name: Build, lint and type-check 59 | run: pnpm turbo build lint type-check 60 | 61 | - name: Check workspaces 62 | run: pnpm manypkg check 63 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | node_modules 5 | .pnp 6 | .pnp.js 7 | 8 | # testing 9 | coverage 10 | 11 | # database 12 | prisma/db.sqlite 13 | prisma/db.sqlite-journal 14 | 15 | # next.js 16 | .next/ 17 | out/ 18 | 19 | # expo 20 | .expo/ 21 | dist/ 22 | 23 | # production 24 | build 25 | 26 | # misc 27 | .DS_Store 28 | *.pem 29 | 30 | # debug 31 | npm-debug.log* 32 | yarn-debug.log* 33 | yarn-error.log* 34 | .pnpm-debug.log* 35 | 36 | # local env files 37 | .env 38 | .env*.local 39 | 40 | # vercel 41 | .vercel 42 | 43 | # typescript 44 | *.tsbuildinfo 45 | 46 | # turbo 47 | .turbo 48 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | # Expo doesn't play nice with pnpm by default. 2 | # The symbolic links of pnpm break the rules of Expo monorepos. 3 | # @link https://docs.expo.dev/guides/monorepos/#common-issues 4 | node-linker=hoisted 5 | 6 | # In order to cache Prisma correctly 7 | public-hoist-pattern[]=*prisma* 8 | 9 | # FIXME: @prisma/client is required by the @acme/auth, 10 | # but we don't want it installed there since it's already 11 | # installed in the @acme/db package 12 | strict-peer-dependencies=false 13 | -------------------------------------------------------------------------------- /.prettierrc.cjs: -------------------------------------------------------------------------------- 1 | /** @type {import("prettier").Config} */ 2 | module.exports = { 3 | arrowParens: "always", 4 | printWidth: 80, 5 | singleQuote: false, 6 | jsxSingleQuote: false, 7 | semi: true, 8 | trailingComma: "all", 9 | tabWidth: 2, 10 | plugins: [require.resolve("prettier-plugin-tailwindcss")], 11 | tailwindConfig: "./packages/config/tailwind", 12 | }; 13 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "esbenp.prettier-vscode", 4 | "dbaeumer.vscode-eslint", 5 | "bradlc.vscode-tailwindcss", 6 | "Prisma.prisma" 7 | ] 8 | } 9 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.codeActionsOnSave": { 3 | "source.fixAll.eslint": true 4 | }, 5 | "editor.defaultFormatter": "esbenp.prettier-vscode", 6 | "editor.formatOnSave": true, 7 | "eslint.rules.customizations": [{ "rule": "*", "severity": "warn" }], 8 | "typescript.tsdk": "node_modules/typescript/lib" 9 | } 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![CI](https://github.com/perkinsjr/t3-turbo-and-clerk/actions/workflows/ci.yml/badge.svg)](https://github.com/perkinsjr/t3-turbo-and-clerk/actions/workflows/ci.yml) 2 | 3 | # Create T3 Turbo with Clerk Authentication 4 | 5 | ## About 6 | 7 | This takes the original create-t3-turbo and adds Clerk authentication allowing you to have one auth package for both Expo and Next.js. You will notice there is no longer an auth package as it is not requried. 8 | 9 | ## Clerk Dashboard Setup 10 | 11 | For this template to work you need to enable Discord as an OAuth provider. You can find the social options under `User & Authentication / Social Providers` in the [Clerk Dashboard](https://dashboard.clerk.dev) 12 | 13 | > If you change any setting here outside of adding Discord, you may need to update your Expo code to handle any requirements you change. 14 | 15 | It uses [Turborepo](https://turborepo.org/) and contains: 16 | 17 | ## Code Layout 18 | 19 | ``` 20 | .github 21 | └─ workflows 22 | └─ CI with pnpm cache setup 23 | .vscode 24 | └─ Recommended extensions and settings for VSCode users 25 | apps 26 | ├─ expo 27 | | ├─ Expo SDK 46 28 | | ├─ React Native using React 18 29 | | ├─ Tailwind using Nativewind 30 | | └─ Typesafe API calls using tRPC 31 | └─ next.js 32 | ├─ Next.js 13 33 | ├─ React 18 34 | ├─ TailwindCSS 35 | └─ E2E Typesafe API Server & Client 36 | packages 37 | ├─ api 38 | | └─ tRPC v10 router definition 39 | └─ db 40 | └─ typesafe db-calls using Prisma 41 | ``` 42 | 43 | ## Quick Start 44 | 45 | To get it running, follow the steps below: 46 | 47 | ### Setup dependencies 48 | 49 | ```diff 50 | # Install dependencies 51 | pnpm i 52 | 53 | # In packages/db/prisma update schema.prisma provider to use sqlite 54 | # or use your own database provider 55 | - provider = "postgresql" 56 | + provider = "sqlite" 57 | 58 | # Configure environment variables. 59 | # There is an `.env.example` in the root directory you can use for reference 60 | cp .env.example .env 61 | 62 | # Push the Prisma schema to your database 63 | pnpm db-push 64 | ``` 65 | 66 | ### Configure Expo app 67 | 68 | ``` 69 | export const CLERK_PUBLISHABLE_KEY = your_publishable_key; 70 | ``` 71 | 72 | ### Configure Expo `dev`-script 73 | 74 | > **Note:** If you want to use a physical phone with Expo Go, just run `pnpm dev` and scan the QR-code. 75 | 76 | #### Use iOS Simulator 77 | 78 | 1. Make sure you have XCode and XCommand Line Tools installed [as shown on expo docs](https://docs.expo.dev/workflow/ios-simulator/). 79 | 2. Change the `dev` script at `apps/expo/package.json` to open the iOS simulator. 80 | 81 | ```diff 82 | + "dev": "expo start --ios", 83 | ``` 84 | 85 | 3. Run `pnpm dev` at the project root folder. 86 | 87 | #### For Android 88 | 89 | 1. Install Android Studio tools [as shown on expo docs](https://docs.expo.dev/workflow/android-studio-emulator/). 90 | 2. Change the `dev` script at `apps/expo/package.json` to open the Android emulator. 91 | 92 | ```diff 93 | + "dev": "expo start --android", 94 | ``` 95 | 96 | 3. Run `pnpm dev` at the project root folder. 97 | 98 | ## Deployment 99 | 100 | ### Next.js 101 | 102 | #### Prerequisites 103 | 104 | _We do not recommend deploying a SQLite database on serverless environments since the data wouldn't be persisted. I provisioned a quick Postgresql database on [Railway](https://railway.app), but you can of course use any other database provider. Make sure the prisma schema is updated to use the correct database._ 105 | 106 | #### Deploy to Vercel 107 | 108 | Let's deploy the Next.js application to [Vercel](https://vercel.com/). If you have ever deployed a Turborepo app there, the steps are quite straightforward. You can also read the [official Turborepo guide](https://vercel.com/docs/concepts/monorepos/turborepo) on deploying to Vercel. 109 | 110 | 1. Create a new project on Vercel, select the `apps/nextjs` folder as the root directory and apply the following build settings: 111 | 112 | Vercel deployment settings 113 | 114 | > The install command filters out the expo package and saves a few second (and cache size) of dependency installation. The build command makes us build the application using Turbo. 115 | 116 | 2. Add your `DATABASE_URL`,`NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY` and `CLERK_SECRET_KEY` environment variable. 117 | 118 | 3. Done! Your app should successfully deploy. Assign your domain and use that instead of `localhost` for the `url` in the Expo app so that your Expo app can communicate with your backend when you are not in development. 119 | 120 | ### Expo 121 | 122 | Deploying your Expo application works slightly differently compared to Next.js on the web. Instead of "deploying" your app online, you need to submit production builds of your app to the app stores, like [Apple App Store](https://www.apple.com/app-store/) and [Google Play](https://play.google.com/store/apps). You can read the full [Distributing your app](https://docs.expo.dev/distribution/introduction/), including best practices, in the Expo docs. 123 | 124 | 1. Let's start by setting up [EAS Build](https://docs.expo.dev/build/introduction/), which is short for Expo Application Services. The build service helps you create builds of your app, without requiring a full native development setup. The commands below are a summary of [Creating your first build](https://docs.expo.dev/build/setup/). 125 | 126 | ```bash 127 | // Install the EAS CLI 128 | $ pnpm add -g eas-cli 129 | 130 | // Log in with your Expo account 131 | $ eas login 132 | 133 | // Configure your Expo app 134 | $ cd apps/expo 135 | $ eas build:configure 136 | ``` 137 | 138 | 2. After the initial setup, you can create your first build. You can build for Android and iOS platforms and use different [**eas.json** build profiles](https://docs.expo.dev/build-reference/eas-json/) to create production builds or development, or test builds. Let's make a production build for iOS. 139 | 140 | ``` 141 | $ eas build --platform ios --profile production 142 | ``` 143 | 144 | > If you don't specify the `--profile` flag, EAS uses the `production` profile by default. 145 | 146 | 3. Now that you have your first production build, you can submit this to the stores. [EAS Submit](https://docs.expo.dev/submit/introduction/) can help you send the build to the stores. 147 | 148 | ``` 149 | $ eas submit --platform ios --latest 150 | ``` 151 | 152 | > You can also combine build and submit in a single command, using `eas build ... --auto-submit`. 153 | 154 | 4. Before you can get your app in the hands of your users, you'll have to provide additional information to the app stores. This includes screenshots, app information, privacy policies, etc. _While still in preview_, [EAS Metadata](https://docs.expo.dev/eas/metadata/) can help you with most of this information. 155 | 156 | 5. Once everything is approved, your users can finally enjoy your app. Let's say you spotted a small typo; you'll have to create a new build, submit it to the stores, and wait for approval before you can resolve this issue. In these cases, you can use EAS Update to quickly send a small bugfix to your users without going through this long process. Let's start by setting up EAS Update. 157 | 158 | The steps below summarize the [Getting started with EAS Update](https://docs.expo.dev/eas-update/getting-started/#configure-your-project) guide. 159 | 160 | ```bash 161 | // Add the `expo-updates` library to your Expo app 162 | $ cd apps/expo 163 | $ pnpm expo install expo-updates 164 | 165 | // Configure EAS Update 166 | $ eas update:configure 167 | ``` 168 | 169 | 6. Before we can send out updates to your app, you have to create a new build and submit it to the app stores. For every change that includes native APIs, you have to rebuild the app and submit the update to the app stores. See steps 2 and 3. 170 | 171 | 7. Now that everything is ready for updates, let's create a new update for `production` builds. With the `--auto` flag, EAS Update uses your current git branch name and commit message for this update. See [How EAS Update works](https://docs.expo.dev/eas-update/how-eas-update-works/#publishing-an-update) for more information. 172 | 173 | ```bash 174 | $ cd apps/expo 175 | $ eas update --auto 176 | ``` 177 | 178 | > Your OTA (Over The Air) updates must always follow the app store's rules. You can't change your app's primary functionality without getting app store approval. But this is a fast way to update your app for minor changes and bug fixes. 179 | 180 | 8. Done! Now that you have created your production build, submitted it to the stores, and installed EAS Update, you are ready for anything! 181 | 182 | ## References 183 | 184 | The stack originates from [create-t3-app](https://github.com/t3-oss/create-t3-app). 185 | 186 | A [blog post](https://jumr.dev/blog/t3-turbo) where I wrote how to migrate a T3 app into this. 187 | -------------------------------------------------------------------------------- /apps/expo/.expo-shared/assets.json: -------------------------------------------------------------------------------- 1 | { 2 | "12bb71342c6255bbf50437ec8f4441c083f47cdb74bd89160c15e4f43e52a1cb": true, 3 | "40b842e832070c58deac6aa9e08fa459302ee3f9da492c7e77d93d2fbf4a56fd": true 4 | } 5 | -------------------------------------------------------------------------------- /apps/expo/app.json: -------------------------------------------------------------------------------- 1 | { 2 | "expo": { 3 | "name": "expo", 4 | "slug": "expo", 5 | "version": "1.0.0", 6 | "orientation": "portrait", 7 | "icon": "./assets/icon.png", 8 | "userInterfaceStyle": "light", 9 | "splash": { 10 | "image": "./assets/splash.png", 11 | "resizeMode": "contain", 12 | "backgroundColor": "#ffffff" 13 | }, 14 | "updates": { 15 | "fallbackToCacheTimeout": 0 16 | }, 17 | "assetBundlePatterns": ["**/*"], 18 | "ios": { 19 | "supportsTablet": true 20 | }, 21 | "android": { 22 | "adaptiveIcon": { 23 | "foregroundImage": "./assets/adaptive-icon.png", 24 | "backgroundColor": "#FFFFFF" 25 | } 26 | }, 27 | "web": { 28 | "favicon": "./assets/favicon.png" 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /apps/expo/assets/adaptive-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/t3dotgg/t3-turbo-and-clerk/26acba90965790180113a59d13ea674bf718dcf4/apps/expo/assets/adaptive-icon.png -------------------------------------------------------------------------------- /apps/expo/assets/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/t3dotgg/t3-turbo-and-clerk/26acba90965790180113a59d13ea674bf718dcf4/apps/expo/assets/favicon.png -------------------------------------------------------------------------------- /apps/expo/assets/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/t3dotgg/t3-turbo-and-clerk/26acba90965790180113a59d13ea674bf718dcf4/apps/expo/assets/icon.png -------------------------------------------------------------------------------- /apps/expo/assets/splash.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/t3dotgg/t3-turbo-and-clerk/26acba90965790180113a59d13ea674bf718dcf4/apps/expo/assets/splash.png -------------------------------------------------------------------------------- /apps/expo/babel.config.js: -------------------------------------------------------------------------------- 1 | module.exports = function (api) { 2 | api.cache(true); 3 | return { 4 | plugins: ["nativewind/babel"], 5 | presets: ["babel-preset-expo"], 6 | }; 7 | }; 8 | -------------------------------------------------------------------------------- /apps/expo/index.ts: -------------------------------------------------------------------------------- 1 | import { registerRootComponent } from "expo"; 2 | 3 | import { App } from "./src/_app"; 4 | 5 | // registerRootComponent calls AppRegistry.registerComponent('main', () => App); 6 | // It also ensures that whether you load the app in Expo Go or in a native build, 7 | // the environment is set up appropriately 8 | registerRootComponent(App); 9 | -------------------------------------------------------------------------------- /apps/expo/metro.config.js: -------------------------------------------------------------------------------- 1 | // Learn more: https://docs.expo.dev/guides/monorepos/ 2 | // eslint-disable-next-line @typescript-eslint/no-var-requires 3 | const { getDefaultConfig } = require('expo/metro-config'); 4 | // eslint-disable-next-line @typescript-eslint/no-var-requires 5 | const path = require('path'); 6 | 7 | const projectRoot = __dirname; 8 | const workspaceRoot = path.resolve(projectRoot, '../..'); 9 | 10 | // Create the default Metro config 11 | const config = getDefaultConfig(projectRoot); 12 | 13 | // Add the additional `cjs` extension to the resolver 14 | config.resolver.sourceExts.push('cjs'); 15 | 16 | // 1. Watch all files within the monorepo 17 | config.watchFolders = [workspaceRoot]; 18 | // 2. Let Metro know where to resolve packages and in what order 19 | config.resolver.nodeModulesPaths = [ 20 | path.resolve(projectRoot, 'node_modules'), 21 | path.resolve(workspaceRoot, 'node_modules'), 22 | ]; 23 | // 3. Force Metro to resolve (sub)dependencies only from the `nodeModulesPaths` 24 | config.resolver.disableHierarchicalLookup = true; 25 | 26 | module.exports = config; 27 | -------------------------------------------------------------------------------- /apps/expo/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@acme/expo", 3 | "version": "0.1.0", 4 | "main": "index.ts", 5 | "scripts": { 6 | "start": "expo start", 7 | "dev": "expo start --ios", 8 | "clean": "rm -rf .expo .turbo node_modules", 9 | "android": "expo start --android", 10 | "ios": "expo start --ios", 11 | "web": "expo start --web", 12 | "lint": "eslint . --ext ts,tsx" 13 | }, 14 | "dependencies": { 15 | "@acme/api": "*", 16 | "@acme/tailwind-config": "*", 17 | "@clerk/clerk-expo": "0.11.3", 18 | "@shopify/flash-list": "^1.4.0", 19 | "@tanstack/react-query": "^4.16.1", 20 | "@trpc/client": "^10.1.0", 21 | "@trpc/react-query": "^10.1.0", 22 | "@trpc/server": "^10.1.0", 23 | "expo": "~47.0.12", 24 | "expo-auth-session": "^3.7.3", 25 | "expo-random": "^13.0.0", 26 | "expo-secure-store": "^12.0.0", 27 | "expo-status-bar": "~1.4.2", 28 | "nativewind": "^2.0.11", 29 | "react": "18.1.0", 30 | "react-dom": "18.1.0", 31 | "react-native": "0.70.5", 32 | "react-native-safe-area-context": "4.4.1", 33 | "react-native-web": "~0.18.10" 34 | }, 35 | "devDependencies": { 36 | "@babel/core": "^7.19.3", 37 | "@babel/preset-env": "^7.19.3", 38 | "@babel/runtime": "^7.19.0", 39 | "@types/react": "^18.0.25", 40 | "@types/react-native": "~0.70.6", 41 | "eslint": "^8.28.0", 42 | "postcss": "^8.4.19", 43 | "tailwindcss": "^3.2.4", 44 | "typescript": "^4.9.3" 45 | }, 46 | "private": true 47 | } 48 | -------------------------------------------------------------------------------- /apps/expo/src/_app.tsx: -------------------------------------------------------------------------------- 1 | import { StatusBar } from "expo-status-bar"; 2 | import React from "react"; 3 | import { SafeAreaProvider } from "react-native-safe-area-context"; 4 | import { TRPCProvider } from "./utils/trpc"; 5 | 6 | import { HomeScreen } from "./screens/home"; 7 | import { SignInSignUpScreen } from "./screens/signin"; 8 | import { ClerkProvider, SignedIn, SignedOut } from "@clerk/clerk-expo"; 9 | import { tokenCache } from "./utils/cache"; 10 | import { CLERK_PUBLISHABLE_KEY } from "./constants"; 11 | 12 | export const App = () => { 13 | return ( 14 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | ); 31 | }; 32 | -------------------------------------------------------------------------------- /apps/expo/src/components/SignInWithOAuth.tsx: -------------------------------------------------------------------------------- 1 | import { useSignUp, useSignIn } from "@clerk/clerk-expo"; 2 | import React from "react"; 3 | import { Button, View } from "react-native"; 4 | 5 | import * as AuthSession from "expo-auth-session"; 6 | 7 | const SignInWithOAuth = () => { 8 | const { isLoaded, signIn, setSession } = useSignIn(); 9 | const { signUp } = useSignUp(); 10 | if (!isLoaded) return null; 11 | 12 | const handleSignInWithDiscordPress = async () => { 13 | try { 14 | const redirectUrl = AuthSession.makeRedirectUri({ 15 | path: "/oauth-native-callback", 16 | }); 17 | 18 | await signIn.create({ 19 | strategy: "oauth_discord", 20 | redirectUrl, 21 | }); 22 | 23 | const { 24 | firstFactorVerification: { externalVerificationRedirectURL }, 25 | } = signIn; 26 | 27 | if (!externalVerificationRedirectURL) 28 | throw "Something went wrong during the OAuth flow. Try again."; 29 | 30 | const authResult = await AuthSession.startAsync({ 31 | authUrl: externalVerificationRedirectURL.toString(), 32 | returnUrl: redirectUrl, 33 | }); 34 | 35 | if (authResult.type !== "success") { 36 | throw "Something went wrong during the OAuth flow. Try again."; 37 | } 38 | 39 | // Get the rotatingTokenNonce from the redirect URL parameters 40 | const { rotating_token_nonce: rotatingTokenNonce } = authResult.params; 41 | 42 | await signIn.reload({ rotatingTokenNonce }); 43 | 44 | const { createdSessionId } = signIn; 45 | 46 | if (createdSessionId) { 47 | // If we have a createdSessionId, then auth was successful 48 | await setSession(createdSessionId); 49 | } else { 50 | // If we have no createdSessionId, then this is a first time sign-in, so 51 | // we should process this as a signUp instead 52 | // Throw if we're not in the right state for creating a new user 53 | if ( 54 | !signUp || 55 | signIn.firstFactorVerification.status !== "transferable" 56 | ) { 57 | throw "Something went wrong during the Sign up OAuth flow. Please ensure that all sign up requirements are met."; 58 | } 59 | 60 | console.log( 61 | "Didn't have an account transferring, following through with new account sign up", 62 | ); 63 | 64 | // Create user 65 | await signUp.create({ transfer: true }); 66 | await signUp.reload({ 67 | rotatingTokenNonce: authResult.params.rotating_token_nonce, 68 | }); 69 | await setSession(signUp.createdSessionId); 70 | } 71 | } catch (err) { 72 | console.log(JSON.stringify(err, null, 2)); 73 | console.log("error signing in", err); 74 | } 75 | }; 76 | 77 | return ( 78 | 79 |