All files authHelper.ts

74.13% Statements 43/58
33.33% Branches 4/12
100% Functions 10/10
74.13% Lines 43/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152                          1x   1x 2x           2x 2x 2x   1x 1x             1x 3x                 3x 3x 3x 2x   2x   1x 2x               1x 1x             1x 1x                       1x 1x 1x                   1x 4x       4x 4x 4x             1x 1x                 1x 1x 1x 1x   1x   1x 2x                               1x 1x     1x 1x 1x        
import {
    AuthFlowType,
    GlobalSignOutCommand,
    InitiateAuthCommand,
    NotAuthorizedException,
    GetUserCommand,
    SignUpCommand,
    UsernameExistsException
} from "@aws-sdk/client-cognito-identity-provider";
import { AdminInfo, ConciergeInfo } from "@deliverables.org/types";
import {client} from "@deliverables.org/config";
import dotenv from "dotenv";
 
dotenv.config();
 
export const signUpConcierge = async (ConciergeInfo:ConciergeInfo ,Password: string) => {
    const params = {
        ClientId: process.env.COGNITO_CONCIERGE_CLIENT_ID,
        Username: ConciergeInfo.email,
        Password: Password,
    }
 
    try {
        const command = new SignUpCommand(params);
        return await client.send(command);
    } catch (error) {
        Eif (error instanceof UsernameExistsException) {
            return 'Username already exists';
        }
        console.error('Error signing up concierge in HELPER:', error);
        throw error; // Re-throw to let controller handle it
    }
}
 
export const signInConcierge = async (Email: string,Password: string) => {
    const params = {
        AuthFlow: "USER_PASSWORD_AUTH" as AuthFlowType,
        ClientId: process.env.COGNITO_CONCIERGE_CLIENT_ID || '',
        AuthParameters: {
            USERNAME: Email,
            PASSWORD: Password,
        }
    }
 
    try {
        const command = new InitiateAuthCommand(params);
        const response = await client.send(command);
        const userInfo = await getUser(response.AuthenticationResult?.AccessToken as string);
 
        return {
            email: Email,
            sub: userInfo?.UserAttributes?.find((attr) => attr.Name === 'sub')?.Value,
            username: userInfo?.UserAttributes?.find((attr) => attr.Name === 'given_name')?.Value,
            accessToken: response.AuthenticationResult?.AccessToken,
            idToken: response.AuthenticationResult?.IdToken,
            refreshToken: response.AuthenticationResult?.RefreshToken,
            tokenType: response.AuthenticationResult?.TokenType,
            type: "concierge"
        };
    } catch (error) {
        Eif (error instanceof NotAuthorizedException) {
            return 'Wrong Username or Password';
        }
        console.error('Error Signing In concierge in HELPER:', error);
        throw error; // Re-throw to let controller handle it
    }
}
 
export const signUpAdmin = async (AdminInfo:AdminInfo ,Password: string) => {
    const params = {
        ClientId: process.env.COGNITO_CLIENT_ID,
        Username: AdminInfo.email,
        Password: Password,
        UserAttributes: [
            {
                Name: 'given_name',
                Value: AdminInfo.givenName
            }
        ]
    }
 
    try {
        const command = new SignUpCommand(params);
        return await client.send(command);
    } catch (error) {
        if (error instanceof UsernameExistsException) {
            return 'Username already exists';
        }
        console.error('Error signing up admin in HELPER:', error);
        throw error; // Re-throw to let controller handle it
    }
}
 
export const getUser = async (AccessToken: string) => {
    const params = {
        AccessToken: AccessToken
    }
 
    try {
        const command = new GetUserCommand(params);
        return await client.send(command);
    } catch (error) {
        console.error('Error retrieving user in HELPER:', error);
        throw error; // Re-throw to let caller handle it
    }
}
 
export const signInAdmin = async (Email: string,Password: string) => {
    const params = {
        AuthFlow: "USER_PASSWORD_AUTH" as AuthFlowType,
        ClientId: process.env.COGNITO_CLIENT_ID || '',
        AuthParameters: {
            USERNAME: Email,
            PASSWORD: Password,
        }
    }
 
    try {
        const command = new InitiateAuthCommand(params);
        const response = await client.send(command);
        const userInfo = await getUser(response.AuthenticationResult?.AccessToken as string);
 
        return {
            email: Email,
            sub: userInfo?.UserAttributes?.find((attr) => attr.Name === 'sub')?.Value,
            username: userInfo?.UserAttributes?.find((attr) => attr.Name === 'given_name')?.Value,
            accessToken: response.AuthenticationResult?.AccessToken,
            idToken: response.AuthenticationResult?.IdToken,
            refreshToken: response.AuthenticationResult?.RefreshToken,
            tokenType: response.AuthenticationResult?.TokenType,
            type: "admin"
        };
    } catch (error) {
        if (error instanceof NotAuthorizedException) {
            return 'Wrong Username or Password';
        }
        console.error('Error Signing In admin in HELPER:', error);
        throw error; // Re-throw to let controller handle it
    }
}
 
export const signOutAdmin = async (AccessToken : string) => {
    const params = {
        AccessToken: AccessToken
    }
    try {
        const command = new GlobalSignOutCommand(params);
        return await client.send(command);
    } catch (error) {
        console.error('Error signing out user in CONFIG:', error);
    }
}