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

fix typings for AccountsClientPassword #825

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions packages/client-password/src/client-password.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AccountsClient } from '@accounts/client';
import { LoginResult, CreateUser } from '@accounts/types';
import { LoginResult } from '@accounts/types';
import { PasswordCreateUserType, PasswordLoginType } from '@accounts/password';
import { AccountsClientPasswordOptions } from './types';

export class AccountsClientPassword {
Expand All @@ -17,16 +18,24 @@ export class AccountsClientPassword {
/**
* Create a new user.
*/
public async createUser(user: CreateUser): Promise<string> {
const hashedPassword = this.hashPassword(user.password);
public async createUser(user: PasswordCreateUserType): Promise<string> {
let hashedPassword = user.password;
if (typeof user.password === 'string') {
// mostly just a type check
hashedPassword = this.hashPassword(user.password);
Copy link

@ducan-ne ducan-ne Dec 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this line is necessary? server stil throw a error if user.password is number, booolean

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of the PR is to fix a possibly null error I believe. It's been a while. But if that's the case hashPassword typings should allow non-string inputs then, which seems a little more messy to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, so the point of this fix is to fix a typescript compile issue. Honestly this fix might not be needed. I forget why I was running into it. Maybe it was just a warning

}
return this.client.transport.createUser({ ...user, password: hashedPassword });
}

/**
* Log the user in with a password.
*/
public async login(user: any): Promise<LoginResult> {
const hashedPassword = this.hashPassword(user.password);
public async login(user: PasswordLoginType): Promise<LoginResult> {
let hashedPassword = user.password;
if (typeof user.password === 'string') {
// mostly just a type check maybe throw if it's not a string?
hashedPassword = this.hashPassword(user.password);
}
return this.client.loginWithService('password', {
...user,
password: hashedPassword,
Expand Down