Skip to content

Commit

Permalink
/user/login errors with code NEW_USER_DENIED_TRY_OTHER_PRODUCT so ui …
Browse files Browse the repository at this point in the history
…can pikc up on it
  • Loading branch information
gobengo committed Nov 10, 2023
1 parent c091143 commit 404fe37
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
8 changes: 4 additions & 4 deletions packages/api/src/error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ export function errorHandler (err, { log }, request) {
return new JSONResponse(error, { status })
}

if (err instanceof HTTPError) {
return new JSONResponse(err, { status })
}

let error = {
code: err.code,
message: err.message
}

if (err instanceof HTTPError) {
return new JSONResponse(error, { status })
}

switch (err.code) {
// Magic SDK errors
case MagicErrors.TokenExpired:
Expand Down
24 changes: 24 additions & 0 deletions packages/api/src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,30 @@ export class MagicTokenRequiredError extends HTTPError {
}
MagicTokenRequiredError.CODE = 'ERROR_MAGIC_TOKEN_REQUIRED'

/**
* Error indicating a new user signup was denied and probably will be indefinitely,
* and the user should try a new product instead.
*/
export class NewUserDeniedTryOtherProductError extends HTTPError {
/**
* @param {string} message
* @param {URL} otherProduct
*/
constructor (message, otherProduct) {
super(message, 403)
this.code = 'NEW_USER_DENIED_TRY_OTHER_PRODUCT'
this.otherProduct = otherProduct
}

toJSON () {
return {
message: this.message,
code: this.code,
otherProduct: this.otherProduct.toString()
}
}
}

export class AgreementsRequiredError extends HTTPError {
/**
* @param {import("./utils/billing-types").Agreement[]} agreements
Expand Down
5 changes: 3 additions & 2 deletions packages/api/src/user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as JWT from './utils/jwt.js'
import { JSONResponse, notFound } from './utils/json-response.js'
import { JWT_ISSUER } from './constants.js'
import { HTTPError, PSAErrorInvalidData, PSAErrorRequiredData, PSAErrorResourceNotFound, RangeNotSatisfiableError } from './errors.js'
import { HTTPError, PSAErrorInvalidData, PSAErrorRequiredData, PSAErrorResourceNotFound, RangeNotSatisfiableError, NewUserDeniedTryOtherProductError } from './errors.js'
import { getTagValue, hasPendingTagProposal, hasTag } from './utils/tags.js'
import {
NO_READ_OR_WRITE,
Expand Down Expand Up @@ -140,7 +140,8 @@ async function loginOrRegister (request, env) {
if (newUserRegistrationIsClosed) {
const user = await env.db.getUser(parsed.issuer, {})
if (!user) {
throw new HTTPError('new user registration is closed. Try creating an account at https://console.web3.storage', 403)
const otherProduct = new URL('https://console.web3.storage/')
throw new NewUserDeniedTryOtherProductError(`new user registration is closed. Try creating an account at ${otherProduct.toString()}`, otherProduct)
}
}

Expand Down
7 changes: 7 additions & 0 deletions packages/api/test/user.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ describe('userLoginPost', function () {
const authToken = createMagicTestModeToken(user.publicAddress, user.claims)
const userPostLoginResponse = await fetch(loginEndpoint, {
headers: {
accept: 'application/json',
authorization: `Bearer ${authToken}`,
contentType: 'application/json'
},
Expand All @@ -599,6 +600,12 @@ describe('userLoginPost', function () {
const responseText = await userPostLoginResponse.text()
assert.equal(userPostLoginResponse.status, 403, 'response status code is 403 forbidden because new user registration is forbidden')
assert.ok(responseText.includes('new user registration is closed'), 'response body indicates new user registration is closed')

const responseJson = JSON.parse(responseText)
assert.equal(typeof responseJson, 'object', 'response can be parsed as json')
assert.ok(responseJson.message.includes('new user registration is closed'), 'response object message indicates new user registration is closed')
assert.equal(responseJson.code, 'NEW_USER_DENIED_TRY_OTHER_PRODUCT')
assert.equal(responseJson.otherProduct, 'https://console.web3.storage/')
})
})
})

0 comments on commit 404fe37

Please sign in to comment.