Giter Site home page Giter Site logo

Web Auth Flow about web HOT 5 OPEN

react-navigation avatar react-navigation commented on April 28, 2024 3
Web Auth Flow

from web.

Comments (5)

chrisdrackett avatar chrisdrackett commented on April 28, 2024 1

@tonypee got this working using the following code for App:

import React from 'react'
import { Platform } from 'react-native'
import { createBrowserApp } from '@react-navigation/web'
import { createAppContainer, createSwitchNavigator } from 'react-navigation'

import { Screen } from './Screen'
import { Screen2 } from './Screen2'

const createApp = Platform.select({
  web: createBrowserApp,
  default: createAppContainer,
})

const AppContainer = createApp(
  createSwitchNavigator(
    {
      Screen: {
          screen: Screen,
          path: '',
      },
      Screen2,
    },
    {
      initialRouteName: 'Screen',
    },
  ),
)

from web.

proof666 avatar proof666 commented on April 28, 2024 1

@chrisdrackett, thank you!

Does some body from react navigation team have recommendations for the 'right' way to accomplish this part of auth flow?

from web.

chrisdrackett avatar chrisdrackett commented on April 28, 2024

so I'm currently getting around this using nested AppContainer's. I'm not sure if this is valid as I'm just getting started, but at the top level of my app I have a normal app container:

import { createSwitchNavigator, createAppContainer } from 'react-navigation'

import { Startup } from './Startup'
import { App } from './App'
import { Auth } from './Auth'

export default createAppContainer(
  createSwitchNavigator(
    {
      Startup,
      Auth,
      App,
    },
    {
      initialRouteName: 'Startup',
    },
  ),
)

then within App I have another:

import React from 'react'
import { Platform } from 'react-native'
import { createBrowserApp } from '@react-navigation/web'
import { createAppContainer, createSwitchNavigator } from 'react-navigation'

import { Screen } from './Screen'
import { Screen2 } from './Screen2'

const createApp = Platform.select({
  web: createBrowserApp,
  default: createAppContainer,
})

const AppContainer = createApp(
  createSwitchNavigator(
    {
      Screen,
      Screen2,
    },
    {
      initialRouteName: 'Screen',
    },
  ),
)

This seems to work well, even if you visit /Screen2 when not authed you'll see my auth screen. Once logged in /Screen2 will be shown as expected.

The only thing I can't seem to figure out is how to make /Screen my index view and work as /. Visiting / shows the correct view, but I don't know how to get back to that view without it ending up as /Screen in the url bar.

from web.

chrisdrackett avatar chrisdrackett commented on April 28, 2024

So I was able to (almost) get this working without two navigators. The issue I'm running into is that if you open the app with a deep URL that is within App it seems that the Startup will be skipped. I found workarounds for this in a native context: https://stackoverflow.com/questions/54350991/react-navigation-deep-linking-with-authentication

but I don't feel these work in a web context.

For now I'm going to keep my nested navigators, but I'm curious if there is a way to support auth without having to "break the rules" :)

from web.

anatula avatar anatula commented on April 28, 2024

Hello, any news/advice regarding the "Web Auth" flow in web? In my Expo project I'm using a Switch Navigator, however in web, as @tonypee said one can go to the "protected" screens without any type of checks. In my project:

const AppSwitchNavigator = createSwitchNavigator(
  {
    AuthLoading: { screen: AuthLoadingScreen, path: "" },
    Auth: { screen: AuthStack, path: "auth" },
    Main: { screen: MainStack, path: "main" },
    Error: ErrorScreen
  },
  {
    initialRouteName: "AuthLoading"
  }
);

And then, my App Container:

const createApp = Platform.select({
  // web: config => createBrowserApp(config, { history: "hash" }),
  web: config => createBrowserApp(config), 
  default: config => createAppContainer(config)
});

export default createApp(AppSwitchNavigator);

I have tried both ways (with history "hash"). I haven't found a lot of documentation about it. I would like to know how to handle this in the web. Thank you!

from web.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.