aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.43k stars 2.13k forks source link

Request to Allow "undefined" for redirectSignOut in OAuthSignOut with Amplify-JS v6 #12716

Open YujiAdachi opened 10 months ago

YujiAdachi commented 10 months ago

Is this related to a new or existing framework?

React Native

Is this related to a new or existing API?

No response

Is this related to another service?

No response

Describe the feature you'd like to request

I would like to request a feature in Amplify-JS v6 that allows undefined to be set for the redirectSignOut in OAuthSignOut. It is important to be able to set this property to undefined when there is no need to redirect after a user logs out. Since the current version does not permit this, I am requesting the implementation of this functionality. Additionally, I would like to point out that in Amplify JS v5, setting undefined for redirectSignOut did not cause any errors.

Describe the solution you'd like

As a solution, I propose a patch file for the handleOAuthSignOut.native.ts file, which is part of the Amplify-JS OAuthSignOut functionality. This patch modifies the conditional statement to work correctly even when the redirectSignOut property is an empty array or undefined.

diff --git a/node_modules/@aws-amplify/auth/src/providers/cognito/utils/oauth/handleOAuthSignOut.native.ts b/node_modules/@aws-amplify/auth/src/providers/cognito/utils/oauth/handleOAuthSignOut.native.ts
index 3cdb031..70d8040 100644
--- a/node_modules/@aws-amplify/auth/src/providers/cognito/utils/oauth/handleOAuthSignOut.native.ts
+++ b/node_modules/@aws-amplify/auth/src/providers/cognito/utils/oauth/handleOAuthSignOut.native.ts
@@ -13,7 +13,7 @@ export const handleOAuthSignOut = async (
 ): Promise<void | OpenAuthSessionResult> => {
    const { isOAuthSignIn, preferPrivateSession } = await store.loadOAuthSignIn();

-   if (isOAuthSignIn) {
+   if (isOAuthSignIn && cognitoConfig.loginWith?.oauth?.redirectSignOut.length) {
        const result = await oAuthSignOutRedirect(
            cognitoConfig,
            preferPrivateSession

Describe alternatives you've considered

I have not thought of any alternatives. If there are any good workarounds, please let me know.

Additional context

"@aws-amplify/react-native": "^1.0.7"
"@aws-amplify/rtn-web-browser": "^1.0.7"
"@react-native-async-storage/async-storage": "^1.21.0"
"@react-native-community/netinfo": "^11.2.1"
"aws-amplify": "^6.0.7"
"react": "18.2.0"
"react-native": "0.73.0"
"react-native-get-random-values": "^1.10.0"

Is this something that you'd be interested in working on?

cwomack commented 10 months ago

Hello, @YujiAdachi 👋 and thank you for opening this issue. I've marked this as a feature request and will review it with our team internally for support to allow redirects to be undefined.

cwomack commented 10 months ago

@YujiAdachi, can you expand on what the use case would be for this? Are you just wanting it to redirect back to the Hosted UI page? If we could understand the goal of this a little better, that would be appreciated!

YujiAdachi commented 10 months ago

Hello @cwomack,

Thank you for considering my feature request. The primary use case for allowing undefined in redirectSignOut is to enable more flexible sign-out flows in applications where a redirect is not necessary or desired post-logout. This would be particularly useful in scenarios where the app needs to remain on the same page or perform other actions after logout, without an automatic redirection to a different URL.

The proposed patch for handleOAuthSignOut.native.ts aims to accommodate this flexibility without impacting existing functionality. It ensures that if redirectSignOut is undefined, the app won't attempt a redirect, aligning with the behavior seen in previous versions of Amplify JS.

I'm attaching a code snippet.

import {Hub} from '@aws-amplify/core';
import {Amplify} from 'aws-amplify';
import {
  AuthUser,
  getCurrentUser,
  signInWithRedirect,
  signOut,
} from 'aws-amplify/auth';
import React, {useEffect, useState} from 'react';
import {Button, StyleSheet, Text, View} from 'react-native';

const awsmobile = {
  ...
  oauth: {
    domain: 'domain.auth.ap-northeast-1.amazoncognito.com',
    scope: [
      'phone',
      'email',
      'openid',
      'profile',
      'aws.cognito.signin.user.admin',
    ],
    redirectSignIn: 'mobile://',
    redirectSignOut: undefined,
    responseType: 'code',
  },
};

Amplify.configure(awsmobile);

const App = () => {
  const [user, setUser] = useState<AuthUser | null>(null);

  useEffect(() => {
    const unsubscribe = Hub.listen('auth', ({payload}) => {
      switch (payload.event) {
        case 'signedIn':
        case 'signedOut':
        case 'signInWithRedirect':
          getUser();
          break;
        default:
          break;
      }
    });
    getUser();

    return unsubscribe;
  }, []);

  const getUser = async (): Promise<void> => {
    try {
      const currentUser = await getCurrentUser();
      setUser(currentUser);
    } catch (error) {
      setUser(null);
    }
  };

  return (
    <View style={styles.container}>
      <Text>AwesomeProject</Text>

      <Text>{user?.username}</Text>
      <Button
        title="Sign In"
        onPress={() => signInWithRedirect({provider: 'Apple'})}
      />
      <Button title="Sign Out" onPress={() => signOut()} />
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

export default App;
YujiAdachi commented 4 months ago

Hello @cwomack,

How is the progress on this issue going? Please let me know if there is anything I can help with.