Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide type def for AuthClass returned from withSSRContext #9968

Closed
3 tasks done
jeferson-sb opened this issue Jun 7, 2022 · 4 comments
Closed
3 tasks done

Provide type def for AuthClass returned from withSSRContext #9968

jeferson-sb opened this issue Jun 7, 2022 · 4 comments
Assignees
Labels
bug Something isn't working SSR Issues related to Server Side Rendering TypeScript Related to TypeScript issues

Comments

@jeferson-sb
Copy link

Before opening, please confirm:

JavaScript Framework

Next.js

Amplify APIs

Authentication

Amplify Categories

auth

Environment information

# Put output below this line
Next.js 12.1.6
React 18.1.0
aws-amplify 4.3.24
node 16.15

Describe the bug

When I use the withSSRContext function I don't get any type definitions from the AuthClass object returned, so I don't know which methods to call when using the Auth module.

When I tried to inspect the type def of withSSRContext this is what I get:
image

Expected behavior

The method withSSRContext should return an object with type definitions for most modules/classes.

Reproduction steps

import { withSSRContext } from 'aws-amplify'
const { Auth } = withSSRContext({ req })

Code Snippet

// Put your code below this line.
const { Auth } = withSSRContext({ req })
const user = await Auth.currentAuthenticatedUser()

Log output

// Put your logs below this line


aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

@chrisbonifacio chrisbonifacio self-assigned this Jun 7, 2022
@chrisbonifacio chrisbonifacio added SSR Issues related to Server Side Rendering TypeScript Related to TypeScript issues bug Something isn't working labels Jun 7, 2022
@chrisbonifacio
Copy link
Member

Hi @jeferson-sb 👋 Thanks for opening this issue. I was able to reproduce it consistently and have labeled it a bug for the JS team. I will note that the team is actively working on improving TypeScript support so this is very helpful to document and track what exactly needs improvement.

@amehi0index
Copy link
Contributor

Hi @jeferson-sb,

To use the AuthClass type on the Auth provided by the withSSRContext utility, you can implement the following workaround solution from @quinnturner found here: #7426

import { withSSRContext } from "aws-amplify";
import { Auth } from '@aws-amplify/auth';

export const getServerSideProps: GetServerSideProps = async (ctx) => {
  const auth = withSSRContext(ctx).Auth as typeof Auth;

  let user: any;
  try {
    user = await auth.currentAuthenticatedUser();
    console.log('user is authenticated');
    // fetch some data and assign it to the data variable
  } catch (err) {
    console.log('error: no authenticated user');
  }
};

@jeferson-sb
Copy link
Author

@amehi0index Thanks, that works!

@tannerabread
Copy link
Contributor

Going to close this as a duplicate of #7426

@tannerabread tannerabread closed this as not planned Won't fix, can't repro, duplicate, stale Oct 7, 2022
@tannerabread tannerabread self-assigned this Dec 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working SSR Issues related to Server Side Rendering TypeScript Related to TypeScript issues
Projects
None yet
Development

No branches or pull requests

4 participants