forked from jagregory/cognito-local
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add more admin features and change password
- Loading branch information
1 parent
b65a5bf
commit 6339917
Showing
7 changed files
with
118 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Services } from "../services"; | ||
import { NotAuthorizedError } from "../errors"; | ||
|
||
interface Input { | ||
UserPoolId: string; | ||
Username: string; | ||
} | ||
|
||
export type AdminConfirmSignUpTarget = (body: Input) => Promise<object | null>; | ||
|
||
export const AdminConfirmSignUp = ({ | ||
cognitoClient, | ||
}: Services): AdminConfirmSignUpTarget => async (body) => { | ||
const { UserPoolId, Username } = body || {}; | ||
const userPool = await cognitoClient.getUserPool(UserPoolId); | ||
const user = await userPool.getUserByUsername(Username); | ||
if (!user) { | ||
throw new NotAuthorizedError(); | ||
} | ||
await userPool.saveUser({ | ||
...user, | ||
UserStatus: "CONFIRMED", | ||
// TODO: Remove existing email_verified attribute? | ||
Attributes: [ | ||
...(user.Attributes || []), | ||
{ | ||
Name: "email_verified", | ||
Value: "true", | ||
}, | ||
], | ||
}); | ||
// TODO: Should possibly return something? | ||
return {}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Services } from "../services"; | ||
import { NotAuthorizedError } from "../errors"; | ||
|
||
interface Input { | ||
UserPoolId: string; | ||
Username: string; | ||
UserAttributes: any; | ||
} | ||
|
||
interface Output { | ||
UserAttributes: any; | ||
} | ||
|
||
export type AdminUpdateUserAttributesTarget = ( | ||
body: Input | ||
) => Promise<Output | null>; | ||
|
||
export const AdminUpdateUserAttributes = ({ | ||
cognitoClient, | ||
}: Services): AdminUpdateUserAttributesTarget => async (body) => { | ||
const { UserPoolId, Username } = body || {}; | ||
const userPool = await cognitoClient.getUserPool(UserPoolId); | ||
const user = await userPool.getUserByUsername(Username); | ||
if (!user) { | ||
throw new NotAuthorizedError(); | ||
} | ||
// TODO: Should save the attributes. | ||
return { | ||
UserAttributes: user.Attributes, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import jwt from "jsonwebtoken"; | ||
import { Services } from "../services"; | ||
import { NotAuthorizedError } from "../errors"; | ||
|
||
interface Input { | ||
AccessToken: string; | ||
PreviousPassword: string; | ||
ProposedPassword: string; | ||
} | ||
|
||
export type ChangePasswordTarget = (body: Input) => Promise<object | null>; | ||
|
||
export const ChangePassword = ({ | ||
cognitoClient, | ||
}: Services): ChangePasswordTarget => async (body) => { | ||
const { AccessToken, PreviousPassword, ProposedPassword } = body || {}; | ||
const claims = jwt.decode(AccessToken) as any; | ||
const userPool = await cognitoClient.getUserPoolForClientId(claims.client_id); | ||
const user = await userPool.getUserByUsername(claims.username); | ||
if (!user) { | ||
throw new NotAuthorizedError(); | ||
} | ||
// TODO: Should check previous password. | ||
await userPool.saveUser({ | ||
...user, | ||
Password: ProposedPassword, | ||
UserLastModifiedDate: new Date().getTime(), | ||
}); | ||
// TODO: Should possibly return something? | ||
return {}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters