@stacksjs/auth 0.70.261 → 0.70.263
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/register.d.ts +28 -1
- package/dist/register.js +1 -1
- package/package.json +3 -3
package/dist/register.d.ts
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
1
|
import type { AuthToken } from './token';
|
|
2
2
|
import type { NewUser } from '@stacksjs/orm';
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Register a user and open a full session for them.
|
|
5
|
+
*
|
|
6
|
+
* Returns the same triple `Auth.loginUsingId()` does. It used to return only
|
|
7
|
+
* the access token, because it called `Auth.createToken()` — a wrapper that
|
|
8
|
+
* mints a refresh token and an expiry and then throws both away. With
|
|
9
|
+
* `config.auth.tokenExpiry` defaulting to one hour, that made every freshly
|
|
10
|
+
* registered account unable to refresh: a client that correctly stores the pair
|
|
11
|
+
* and exchanges it at `/auth/refresh` worked for everyone except the user who
|
|
12
|
+
* had just signed up, who was logged out an hour into their first session
|
|
13
|
+
* (stacksjs/stacks#2212).
|
|
14
|
+
*
|
|
15
|
+
* Additive, so `const { token } = await register(...)` is unaffected.
|
|
16
|
+
*/
|
|
17
|
+
export declare function register(credentials: NewUser): Promise<RegistrationResult>;
|
|
18
|
+
/**
|
|
19
|
+
* What a successful registration hands back: a complete session, matching
|
|
20
|
+
* `Auth.loginUsingId()`.
|
|
21
|
+
*
|
|
22
|
+
* Named rather than inlined on the signature so consumers can type the result,
|
|
23
|
+
* and because pickier's unused-parameter analysis mis-reads a multi-line return
|
|
24
|
+
* annotation and flags `credentials` as unused.
|
|
25
|
+
*/
|
|
26
|
+
export declare interface RegistrationResult {
|
|
27
|
+
token: AuthToken
|
|
28
|
+
refreshToken?: string
|
|
29
|
+
expiresIn?: number
|
|
30
|
+
}
|
package/dist/register.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{config}from"@stacksjs/config";import{db}from"@stacksjs/database";import{HttpError}from"@stacksjs/error-handling";import{User}from"@stacksjs/orm";import{makeHash}from"@stacksjs/security";import{Auth}from"./authentication";import{isUniqueViolation}from"./rbac-store-bqb";function duplicateEmailError(){if(config.auth?.registration?.preventEnumeration)return new HttpError(422,"Registration could not be completed. Please check your details and try again.");return new HttpError(409,"Email already exists")}const EMAIL_RE=/^[^\s@]+@[^\s@]+\.[^\s@]+$/;export async function register(credentials){const{email,password,name}=credentials;if(typeof email!=="string"||email.length>254||!EMAIL_RE.test(email))throw new HttpError(422,"Email address is invalid");if(typeof password!=="string"||password.length<8)throw new HttpError(422,"Password must be at least 8 characters");const hashedPassword=await makeHash(password,{algorithm:"bcrypt"}),userId=await db.transaction(async(rawTrx)=>{const trx=rawTrx;if(await trx.selectFrom("users").where("email","=",email).selectAll().executeTakeFirst())throw duplicateEmailError();try{await trx.insertInto("users").values({email,password:hashedPassword,name}).execute()}catch(err){if(isUniqueViolation(err))throw duplicateEmailError();throw err}const created=await trx.selectFrom("users").where("email","=",email).selectAll().executeTakeFirst();if(!created)throw Error("Failed to retrieve created user");return Number(created.id)}),user=await User.find(userId);if(!user)throw Error("Failed to retrieve created user");
|
|
1
|
+
import{config}from"@stacksjs/config";import{db}from"@stacksjs/database";import{HttpError}from"@stacksjs/error-handling";import{User}from"@stacksjs/orm";import{makeHash}from"@stacksjs/security";import{Auth}from"./authentication";import{isUniqueViolation}from"./rbac-store-bqb";function duplicateEmailError(){if(config.auth?.registration?.preventEnumeration)return new HttpError(422,"Registration could not be completed. Please check your details and try again.");return new HttpError(409,"Email already exists")}const EMAIL_RE=/^[^\s@]+@[^\s@]+\.[^\s@]+$/;export async function register(credentials){const{email,password,name}=credentials;if(typeof email!=="string"||email.length>254||!EMAIL_RE.test(email))throw new HttpError(422,"Email address is invalid");if(typeof password!=="string"||password.length<8)throw new HttpError(422,"Password must be at least 8 characters");const hashedPassword=await makeHash(password,{algorithm:"bcrypt"}),userId=await db.transaction(async(rawTrx)=>{const trx=rawTrx;if(await trx.selectFrom("users").where("email","=",email).selectAll().executeTakeFirst())throw duplicateEmailError();try{await trx.insertInto("users").values({email,password:hashedPassword,name}).execute()}catch(err){if(isUniqueViolation(err))throw duplicateEmailError();throw err}const created=await trx.selectFrom("users").where("email","=",email).selectAll().executeTakeFirst();if(!created)throw Error("Failed to retrieve created user");return Number(created.id)}),user=await User.find(userId);if(!user)throw Error("Failed to retrieve created user");const{plainTextToken,refreshToken,expiresIn}=await Auth.createTokenForUser(user,{name:"user-auth-token"});return{token:plainTextToken,refreshToken,expiresIn}}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@stacksjs/auth",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"sideEffects": false,
|
|
5
|
-
"version": "0.70.
|
|
5
|
+
"version": "0.70.263",
|
|
6
6
|
"description": "A more simplistic way to authenticate.",
|
|
7
7
|
"author": "Chris Breuer",
|
|
8
8
|
"contributors": [
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
63
|
"better-dx": "^0.2.17",
|
|
64
|
-
"@stacksjs/error-handling": "0.70.
|
|
65
|
-
"@stacksjs/router": "0.70.
|
|
64
|
+
"@stacksjs/error-handling": "0.70.263",
|
|
65
|
+
"@stacksjs/router": "0.70.263"
|
|
66
66
|
}
|
|
67
67
|
}
|