Skip to content

Unable to Redirect to Keycloak Login Page #124

@Teut2711

Description

@Teut2711

I'm currently using the package in the following manner. My homepage consists of a single image that triggers a mock API call and should navigate to the login page. The login page contains username and password fields. I'm using the latest version of Keycloak deployed at my website https://kk.irasus.com/.

The issue I'm facing is that instead of being redirected to the Keycloak login page, I'm redirected to my custom login page. It seems that wrapping my login page component with the provided Keycloak component has no effect, as I still see my custom login page. My intention is to replace my custom login page with the Keycloak login page. Removing the form on the login page is planned once Keycloak integration works.

Could you please assist me in resolving this issue and ensuring that I'm correctly redirected to the Keycloak login page?

Thank you!

import React, {useState} from 'react';
import {StyleSheet, View} from 'react-native';
import {
  RNKeycloak,
  ReactNativeKeycloakProvider,
  useKeycloak,
} from '@react-keycloak/native';

import {HStack} from '@react-native-material/core';
import {Text, Switch} from 'react-native-paper';
import {VStack} from '@react-native-material/core';
import {TouchableOpacity} from 'react-native';

import {SubmitButton} from '~/__common__/components/Button';
import AuthProviders from '~/__common__/components/AuthProviders';
import Fields from '~/login/components/Fields';

const Authenticate = ({children}) => {
  const keycloakConfig = new RNKeycloak({
    url: 'https://kk.irasus.com',
    realm: 'master',
    clientId: 'esselenergy',
  });

  const keycloakInitOptions = {
    flow: 'implicit',
    // if you need to customize "react-native-inappbrowser-reborn" View you can use the following attribute
    inAppBrowserOptions: {},
    onLoad: 'check-sso',
    redirectUri: 'energymetrics://metricsdisplay',
    useNonce: true,
  };
  React.useEffect(() => {
    console.log(keycloakConfig);
  });
  return (
    <ReactNativeKeycloakProvider
      authClient={keycloakConfig}
      initOptions={keycloakInitOptions}>
      {children}
    </ReactNativeKeycloakProvider>
  );
};

const Login = ({navigation}) => {
  const [isEnabled, setIsEnabled] = useState(false);
  const toggleSwitch = () => setIsEnabled(previousState => !previousState);

  const [keycloak, initialized] = useKeycloak();
  const handleLogin = e => {
    // if (keycloak.authenticated) {
    //   console.log('Authenticated');
    //   navigation.navigate('VEHICLEIDENTITYINFO');
    // } else {
    //   console.log('Not Authenticated');
    //   console.log(keycloak.authServerUrl);
    // }
  };

  return (
    <Authenticate>
      <VStack fill center p={10} spacing={10}>
        <Text variant="displaySmall">Log In</Text>
        <Fields />
        <HStack m={4} spacing={30}>
          <HStack m={4} spacing={6}>
            <Switch
              trackColor={{false: '#E0E0E0', true: '#4CAF50'}}
              thumbColor={isEnabled ? '#FFFFFF' : '#FFFFFF'}
              ios_backgroundColor="#9E9E9E"
              onValueChange={toggleSwitch}
              value={isEnabled}
            />
            <Text>Remember me</Text>
          </HStack>
          <HStack m={4} spacing={6}>
            <Text style={styles.underline}>Forgot Password?</Text>{' '}
          </HStack>
        </HStack>
        <SubmitButton title="Login" onPress={handleLogin} />
        <HStack spacing={5}>
          <View style={styles.divider} />
          <Text>Or login with</Text>
          <View style={styles.divider} />
        </HStack>
        <AuthProviders navigation={navigation} />
        <HStack m={4} spacing={6}>
          <Text>Don’t have an account?</Text>
          <TouchableOpacity
            onPress={() => navigation.navigate('SIGNUP')}
            style={styles.accessApp}>
            <Text style={styles.accessThrough}>Signup</Text>
          </TouchableOpacity>
        </HStack>
        <HStack m={4} spacing={6}>
          <Text>Want to login with OTP?</Text>
          <TouchableOpacity
            onPress={() => navigation.navigate('OTP')}
            style={styles.accessApp}>
            <Text style={styles.accessThrough}>Click here</Text>
          </TouchableOpacity>
        </HStack>
      </VStack>
    </Authenticate>
  );
};

const styles = StyleSheet.create({
  underline: {textDecorationLine: 'underline'},

  divider: {
    flex: 1,
    borderBottomWidth: 1,
    borderBottomColor: '#A09F99',
    marginBottom: 9,
  },
  accessApp: {
    borderRadius: 2,
    backgroundColor: 'rgba(0,0,0,0)',
    alignSelf: 'flex-start',
  },
  accessThrough: {
    color: '#2FAD65',
    fontWeight: 'bold',
  },
});

export default Login;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions