Skip to content

Commit

Permalink
fix reset password request
Browse files Browse the repository at this point in the history
Signed-off-by: Musilah <[email protected]>
  • Loading branch information
Musilah committed Apr 3, 2024
1 parent efbd6c7 commit 2b14fc4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
10 changes: 5 additions & 5 deletions examples/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ mySdk.users
.Create({
name: '<name>',
credentials: {
identity: '[email protected]',
secret: '12345678'
identity: '<useremail>',
secret: '<password>'
}
})
.then((response: any) => {
Expand Down Expand Up @@ -45,7 +45,7 @@ mySdk.users.UserProfile(
})

mySdk.users.CreateToken(
{ identity: '[email protected]', secret: '12345678' }
{ identity: '<identity>', secret: '<password>' }
)
.then((response: any) => {
console.log('response: ', response)
Expand All @@ -55,7 +55,7 @@ mySdk.users.CreateToken(
})

mySdk.users.RefreshToken(
{ identity: 'userId' },
{ identity: '<userId>' },
'<refreshToken>'
)
.then((response: any) => {
Expand Down Expand Up @@ -99,7 +99,7 @@ mySdk.users.UpdateUserTags(
})

mySdk.users.UpdateUserRole(
{ id: '<userId>', role: '<user_role>' },
{ id: '<userId>', role: '<userRole>' },
'<token>'
)
.then((response: any) => {
Expand Down
4 changes: 4 additions & 0 deletions src/defs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export interface Status {
status: string
}

export interface Response {
status: number
message?: string
}
export interface QueryParams {
total?: number
offset?: number
Expand Down
6 changes: 4 additions & 2 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const defaultUrl = 'http://localhost'
interface SDKConfig {
usersUrl?: string
thingsUrl?: string
hostUrl?: string
}

class SDK {
Expand All @@ -14,9 +15,10 @@ class SDK {

constructor ({
usersUrl = defaultUrl,
thingsUrl = defaultUrl
thingsUrl = defaultUrl,
hostUrl = defaultUrl
}: SDKConfig = {}) {
this.users = new Users(usersUrl, thingsUrl)
this.users = new Users({ usersUrl, thingsUrl, hostUrl })
this.things = new Things(thingsUrl)
}
}
Expand Down
33 changes: 21 additions & 12 deletions src/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
type Login,
type QueryParams,
type Token,
type Status
type Response

} from './defs'

Expand All @@ -24,14 +24,22 @@ export default class Users {
* @returns {Object} - Users object.
*/
private readonly usersUrl: URL
private readonly thingsUrl: URL
private readonly thingsUrl?: URL
private readonly hostUrl: URL
private readonly contentType: string
private readonly usersEndpoint: string
private readonly userError: Errors

public constructor (usersUrl: string, thingsUrl: string) {
public constructor ({ usersUrl, thingsUrl, hostUrl }: { usersUrl: string, thingsUrl?: string, hostUrl?: string }) {
this.usersUrl = new URL(usersUrl)
this.thingsUrl = new URL(thingsUrl)
if (thingsUrl !== undefined) {
this.thingsUrl = new URL(thingsUrl)
}
if (hostUrl !== undefined) {
this.hostUrl = new URL(hostUrl)
} else {
this.hostUrl = new URL('')
}
this.contentType = 'application/json'
this.usersEndpoint = 'users'
this.userError = new Errors()
Expand Down Expand Up @@ -741,7 +749,7 @@ export default class Users {
}
}

public async ResetPasswordRequest (email: string): Promise<Status> {
public async ResetPasswordRequest (email: string): Promise<Response> {
// Sends a request to reset a password
/**
* @method ResetPasswordRequest - Sends a request
Expand All @@ -759,7 +767,8 @@ export default class Users {
const options: RequestInit = {
method: 'POST',
headers: {
'Content-Type': this.contentType
'Content-Type': this.contentType,
Referer: this.hostUrl.toString()
},
body: JSON.stringify({ email })
}
Expand All @@ -772,8 +781,8 @@ export default class Users {
const errorRes = await response.json()
throw this.userError.HandleError(errorRes.error, response.status)
}
const status: Status = await response.json()
return status
const resetRequestResponse: Response = { status: response.status, message: 'Email with reset link is sent' }
return resetRequestResponse
} catch (error) {
throw error
}
Expand All @@ -783,7 +792,7 @@ export default class Users {
password: string,
confPass: string,
token: string
): Promise<Status> {
): Promise<Response> {
// Resets a user password
/**
* @method ResetPassword - Resets a password.
Expand All @@ -805,7 +814,7 @@ export default class Users {
headers: {
'Content-Type': this.contentType
},
body: JSON.stringify({ password, confirm_password: confPass, token })
body: JSON.stringify({ token, password, confirm_password: confPass })
}
try {
const response = await fetch(
Expand All @@ -816,8 +825,8 @@ export default class Users {
const errorRes = await response.json()
throw this.userError.HandleError(errorRes.error, response.status)
}
const status: Status = await response.json()
return status
const resetResponse: Response = { status: response.status, message: 'Password reset succesfully' }
return resetResponse
} catch (error) {
throw error
}
Expand Down

0 comments on commit 2b14fc4

Please sign in to comment.