Skip to content

Commit

Permalink
Updated to amplify v6. Login not fully implemented again though
Browse files Browse the repository at this point in the history
  • Loading branch information
realworld666 committed Jan 1, 2025
1 parent 4aebebe commit dc5c2e1
Show file tree
Hide file tree
Showing 4 changed files with 4,811 additions and 1,344 deletions.
85 changes: 55 additions & 30 deletions server/public/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CognitoUser } from 'amazon-cognito-identity-js';
import { Auth } from '@aws-amplify/auth';
import {ConfigControllerService, ConfigResponse, RegisterData, UserControllerService} from "../generated";

import { Amplify } from "aws-amplify";
import { confirmSignIn, fetchAuthSession, signIn } from "aws-amplify/auth";
const mockSetupLocalStorageKey = 'mock-auth';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand All @@ -17,10 +16,13 @@ export function configureCognito() {
}

function doConfigure(config: ConfigResponse) {
Auth.configure({
region: config.auth.region,
userPoolId: config.auth.userPoolId,
userPoolWebClientId: config.auth.clientId,
Amplify.configure({
Auth:{
Cognito:{
userPoolId: config.auth.userPoolId,
userPoolClientId: config.auth.clientId
}
}
});
}

Expand All @@ -35,32 +37,44 @@ export async function requestCode() {
email: email,
action: RegisterData.action.REGISTER,
};

try {
const user = await UserControllerService.registerUser(data);

console.log(`Email: '${email}'`);
const config = await ConfigControllerService.getConfig();
doConfigure(config);
window.loggedInUser = await Auth.signIn(email.toLowerCase());

const { isSignedIn, nextStep } = await signIn({ username: email.toLowerCase() });

if (isSignedIn) {
console.log('User signed in successfully');
} else if (nextStep.signInStep === 'CONFIRM_SIGN_IN_WITH_CUSTOM_CHALLENGE') {
console.log('Custom challenge required');
} else {
console.log('Unexpected sign-in step:', nextStep.signInStep);
}

// Switch forms
const emailForm = (document.querySelector<HTMLInputElement>('#form1')!.style.display = 'none');
const codeForm = (document.querySelector<HTMLInputElement>('#form2')!.style.display = 'initial');
document.querySelector<HTMLElement>('#form1')!.style.display = 'none';
document.querySelector<HTMLElement>('#form2')!.style.display = 'initial';
} catch (e) {
alert(e.message);
console.error('Error during registration or sign-in:', e);
alert(e.message || 'An error occurred during registration or sign-in');
throw e;
}
}

export async function validateCode() {
const code = document.querySelector<HTMLInputElement>('#code')?.value ?? '';

if (window.loggedInUser === undefined) throw new Error('Logged in user not set');

// Retrieve AWS user
const user: CognitoUser = await Auth.sendCustomChallengeAnswer(window.loggedInUser, code);

return user.getSignInUserSession() !== null;
try {
const { isSignedIn } = await confirmSignIn({ challengeResponse: code });
return isSignedIn;
} catch (error) {
console.error('Error validating code:', error);
return false;
}
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand All @@ -70,26 +84,37 @@ export async function performLogin(): Promise<void> {
alert('Could not validate the code');
return;
}
try {
const { tokens } = await fetchAuthSession();
if (!tokens) {
console.log('No active session found');
return;
}
const idToken = tokens.idToken;
const jwtToken = idToken.toString();

const queryParams = new URLSearchParams(window.location.search);
const redirectUri = queryParams.get('redirect_uri');
const state = queryParams.get('state');
if (!redirectUri || !state) {
console.error('Missing required query parameters');
return;
}

// get jwt token
const session = await tryGetCurrentSession();
if (session === undefined) {
return undefined;
const redirectUrl = `${redirectUri}#access_token=${encodeURIComponent(jwtToken)}&state=${encodeURIComponent(state)}&response_type=token`;
window.location.href = redirectUrl;
}
catch (error) {
console.error('Error fetching session:', error);
}
const accessToken = session.getIdToken();
const credentials = accessToken.getJwtToken();

const queryParams = getQueryParams(window.location.href);
const redirectUrl = `${queryParams.redirect_uri}#access_token=${encodeURIComponent(
credentials
)}&state=${encodeURIComponent(queryParams.state)}&response_type=token`;
window.location.href = redirectUrl;
}

async function tryGetCurrentSession() {
try {
return await Auth.currentSession();
const { tokens } = await fetchAuthSession();
return tokens;
} catch (err) {
console.error('Error fetching auth session:', err);
return undefined;
}
}
Expand Down
3 changes: 1 addition & 2 deletions server/public/globals.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// noinspection ES6ConvertVarToLetConst

import { CognitoUser } from 'amazon-cognito-identity-js';

declare global {
var loggedInUser: CognitoUser | undefined;
var loggedInUser: any | undefined;
}
Loading

0 comments on commit dc5c2e1

Please sign in to comment.