1
0
mirror of synced 2026-02-04 03:01:17 -05:00

Compare commits

...

4 Commits

Author SHA1 Message Date
Brandon Bayer
00bd849eef 2.0.0-alpha.5 2022-04-15 09:03:01 -04:00
Dillon Raphael
c8db1a0b7e Toolkit template (#3298)
* migrate generate command

* tidy up the help command

* Convert legacy full template to new working example

* change db to prisma for templates

* change toolkit-app to use useQuery instead of invoke

* new app & generator templates working for npm only

* fixed npm install

* set blitz config package version

* assert session id

* add type assert to template
2022-04-15 08:35:47 -04:00
Brandon Bayer
fb32903bf9 2.0.0-alpha.4 fix more cli stuff 2022-04-14 21:13:57 -04:00
Brandon Bayer
8dedca1a29 2.0.0-alpha.3 - fix cli esm problem 2022-04-14 21:04:51 -04:00
149 changed files with 3633 additions and 1642 deletions

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
downgrade pkg-dir to non-esm only version

View File

@@ -12,7 +12,14 @@
"@blitzjs/rpc": "2.0.0-alpha.0",
"@blitzjs/config": "0.0.0",
"@blitzjs/generator": "2.0.0-alpha.0",
"template": "0.0.0"
"template": "0.0.0",
"toolkit-app": "1.0.0"
},
"changesets": ["ninety-pets-heal", "poor-peas-lick"]
"changesets": [
"nine-onions-admire",
"ninety-pets-heal",
"poor-peas-lick",
"ten-rivers-burn",
"twenty-beans-pump"
]
}

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
fix more cli problems

View File

@@ -0,0 +1,10 @@
---
"blitz": patch
"@blitzjs/auth": patch
"@blitzjs/next": patch
"@blitzjs/rpc": patch
"@blitzjs/config": patch
"@blitzjs/generator": patch
---
new app template

View File

@@ -0,0 +1,11 @@
# https://EditorConfig.org
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

3
apps/toolkit-app/.env Normal file
View File

@@ -0,0 +1,3 @@
# This env file should be checked into source control
# This is the place for default values for all environments
# Values in `.env.local` and `.env.production` will override these values

View File

@@ -0,0 +1 @@
module.exports = require("@blitzjs/config/eslint")

View File

@@ -0,0 +1,9 @@
.gitkeep
.env*
*.ico
*.lock
db/migrations
.next
.yarn
.pnp.*
node_modules

View File

@@ -0,0 +1,12 @@
# toolkit-app
## 1.0.1-alpha.0
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.5
- @blitzjs/auth@2.0.0-alpha.5
- @blitzjs/next@2.0.0-alpha.5
- @blitzjs/rpc@2.0.0-alpha.5
- @blitzjs/config@2.0.0-alpha.5

175
apps/toolkit-app/README.md Normal file
View File

@@ -0,0 +1,175 @@
TODO
[![Blitz.js](https://raw.githubusercontent.com/blitz-js/art/master/github-cover-photo.png)](https://blitzjs.com)
This is a [Blitz.js](https://github.com/blitz-js/blitz) app.
# ****name****
## Getting Started
Run your app in the development mode.
```
blitz dev
```
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
## Environment Variables
Ensure the `.env.local` file has required environment variables:
```
DATABASE_URL=postgresql://<YOUR_DB_USERNAME>@localhost:5432/__name__
```
Ensure the `.env.test.local` file has required environment variables:
```
DATABASE_URL=postgresql://<YOUR_DB_USERNAME>@localhost:5432/__name___test
```
## Tests
Runs your tests using Jest.
```
yarn test
```
Blitz comes with a test setup using [Jest](https://jestjs.io/) and [react-testing-library](https://testing-library.com/).
## Commands
Blitz comes with a powerful CLI that is designed to make development easy and fast. You can install it with `npm i -g blitz`
```
blitz [COMMAND]
dev Start a development server
build Create a production build
start Start a production server
export Export your Blitz app as a static application
prisma Run prisma commands
generate Generate new files for your Blitz project
console Run the Blitz console REPL
install Install a recipe
help Display help for blitz
test Run project tests
```
You can read more about it on the [CLI Overview](https://blitzjs.com/docs/cli-overview) documentation.
## What's included?
Here is the starting structure of your app.
```
__name__
├── app/
│ ├── api/
│ ├── auth/
│ │ ├── components/
│ │ │ ├── LoginForm.tsx
│ │ │ └── SignupForm.tsx
│ │ ├── mutations/
│ │ │ ├── changePassword.ts
│ │ │ ├── forgotPassword.test.ts
│ │ │ ├── forgotPassword.ts
│ │ │ ├── login.ts
│ │ │ ├── logout.ts
│ │ │ ├── resetPassword.test.ts
│ │ │ ├── resetPassword.ts
│ │ │ └── signup.ts
│ │ ├── pages/
│ │ │ ├── forgot-password.tsx
│ │ │ ├── login.tsx
│ │ │ ├── reset-password.tsx
│ │ │ └── signup.tsx
│ │ └── validations.ts
│ ├── core/
│ │ ├── components/
│ │ │ ├── Form.tsx
│ │ │ └── LabeledTextField.tsx
│ │ ├── hooks/
│ │ │ └── useCurrentUser.ts
│ │ └── layouts/
│ │ └── Layout.tsx
│ ├── pages/
│ │ ├── _app.tsx
│ │ ├── _document.tsx
│ │ ├── 404.tsx
│ │ ├── index.test.tsx
│ │ └── index.tsx
│ └── users/
│ └── queries/
│ └── getCurrentUser.ts
├── db/
│ ├── migrations/
│ ├── index.ts
│ ├── schema.prisma
│ └── seeds.ts
├── integrations/
├── mailers/
│ └── forgotPasswordMailer.ts
├── public/
│ ├── favicon.ico
│ └── logo.png
├── test/
│ ├── setup.ts
│ └── utils.tsx
├── .eslintrc.js
├── babel.config.js
├── blitz.config.ts
├── jest.config.ts
├── package.json
├── README.md
├── tsconfig.json
└── types.ts
```
These files are:
- The `app/` folder is a container for most of your project. This is where youll put any pages or API routes.
- `db/` is where your database configuration goes. If youre writing models or checking migrations, this is where to go.
- `public/` is a folder where you will put any static assets. If you have images, files, or videos which you want to use in your app, this is where to put them.
- `integrations/` is a folder to put all third-party integrations like with Stripe, Sentry, etc.
- `test/` is a folder where you can put test utilities and integration tests.
- `package.json` contains information about your dependencies and devDependencies. If youre using a tool like `npm` or `yarn`, you wont have to worry about this much.
- `tsconfig.json` is our recommended setup for TypeScript.
- `.babel.config.js`, `.eslintrc.js`, `.env`, etc. ("dotfiles") are configuration files for various bits of JavaScript tooling.
- `blitz.config.ts` is for advanced custom configuration of Blitz. [Here you can learn how to use it](https://blitzjs.com/docs/blitz-config).
- `jest.config.js` contains config for Jest tests. You can [customize it if needed](https://jestjs.io/docs/en/configuration).
You can read more about it in the [File Structure](https://blitzjs.com/docs/file-structure) section of the documentation.
### Tools included
Blitz comes with a set of tools that corrects and formats your code, facilitating its future maintenance. You can modify their options and even uninstall them.
- **ESLint**: It lints your code: searches for bad practices and tell you about it. You can customize it via the `.eslintrc.js`, and you can install (or even write) plugins to have it the way you like it. It already comes with the [`blitz`](https://github.com/blitz-js/blitz/tree/canary/packages/eslint-config) config, but you can remove it safely. [Learn More](https://blitzjs.com/docs/eslint-config).
- **Husky**: It adds [githooks](https://git-scm.com/docs/githooks), little pieces of code that get executed when certain Git events are triggerd. For example, `pre-commit` is triggered just before a commit is created. You can see the current hooks inside `.husky/`. If are having problems commiting and pushing, check out ther [troubleshooting](https://typicode.github.io/husky/#/?id=troubleshoot) guide. [Learn More](https://blitzjs.com/docs/husky-config).
- **Prettier**: It formats your code to look the same everywhere. You can configure it via the `.prettierrc` file. The `.prettierignore` contains the files that should be ignored by Prettier; useful when you have large files or when you want to keep a custom formatting. [Learn More](https://blitzjs.com/docs/prettier-config).
## Learn more
Read the [Blitz.js Documentation](https://blitzjs.com/docs/getting-started) to learn more.
The Blitz community is warm, safe, diverse, inclusive, and fun! Feel free to reach out to us in any of our communication channels.
- [Website](https://blitzjs.com)
- [Discord](https://blitzjs.com/discord)
- [Report an issue](https://github.com/blitz-js/blitz/issues/new/choose)
- [Forum discussions](https://github.com/blitz-js/blitz/discussions)
- [How to Contribute](https://blitzjs.com/docs/contributing)
- [Sponsor or donate](https://github.com/blitz-js/blitz#sponsors-and-donations)

View File

@@ -0,0 +1,58 @@
import { AuthenticationError, PromiseReturnType } from "blitz"
import Link from "next/link"
import { LabeledTextField } from "app/core/components/LabeledTextField"
import { Form, FORM_ERROR } from "app/core/components/Form"
import login from "app/auth/mutations/login"
import { Login } from "app/auth/validations"
import { useMutation } from "@blitzjs/rpc"
type LoginFormProps = {
onSuccess?: (user: PromiseReturnType<typeof login>) => void
}
export const LoginForm = (props: LoginFormProps) => {
const [loginMutation] = useMutation(login)
return (
<div>
<h1>Login</h1>
<Form
submitText="Login"
schema={Login}
initialValues={{ email: "", password: "" }}
onSubmit={async (values) => {
try {
const user = await loginMutation(values)
props.onSuccess?.(user)
} catch (error: any) {
if (error instanceof AuthenticationError) {
return { [FORM_ERROR]: "Sorry, those credentials are invalid" }
} else {
return {
[FORM_ERROR]:
"Sorry, we had an unexpected error. Please try again. - " + error.toString(),
}
}
}
}}
>
<LabeledTextField name="email" label="Email" placeholder="Email" />
<LabeledTextField name="password" label="Password" placeholder="Password" type="password" />
<div>
<Link href="/auth/forgot-password" passHref>
<a>Forgot your password?</a>
</Link>
</div>
</Form>
<div style={{ marginTop: "1rem" }}>
Or{" "}
<Link href="/auth/signup" passHref>
<a>Sign Up</a>
</Link>
</div>
</div>
)
}
export default LoginForm

View File

@@ -0,0 +1,42 @@
import { LabeledTextField } from "app/core/components/LabeledTextField"
import { Form, FORM_ERROR } from "app/core/components/Form"
import signup from "app/auth/mutations/signup"
import { Signup } from "app/auth/validations"
import { useMutation } from "@blitzjs/rpc"
type SignupFormProps = {
onSuccess?: () => void
}
export const SignupForm = (props: SignupFormProps) => {
const [signupMutation] = useMutation(signup)
return (
<div>
<h1>Create an Account</h1>
<Form
submitText="Create Account"
schema={Signup}
initialValues={{ email: "", password: "" }}
onSubmit={async (values) => {
try {
await signupMutation(values)
props.onSuccess?.()
} catch (error: any) {
if (error.code === "P2002" && error.meta?.target?.includes("email")) {
// This error comes from Prisma
return { email: "This email is already being used" }
} else {
return { [FORM_ERROR]: error.toString() }
}
}
}}
>
<LabeledTextField name="email" label="Email" placeholder="Email" />
<LabeledTextField name="password" label="Password" placeholder="Password" type="password" />
</Form>
</div>
)
}
export default SignupForm

View File

@@ -0,0 +1,26 @@
import { NotFoundError, Ctx } from "blitz"
import { prisma } from "db"
import { authenticateUser } from "./login"
import { ChangePassword } from "../validations"
import { SecurePassword } from "@blitzjs/auth"
export default async function changePassword(input, ctx: Ctx) {
ChangePassword.parse(input)
ctx.session.$isAuthorized()
const user = await prisma.user.findFirst({
where: {
id: ctx.session.userId as number,
},
})
if (!user) throw new NotFoundError()
await authenticateUser(user.email, input.currentPassword)
const hashedPassword = await SecurePassword.hash(input.newPassword.trim())
await prisma.user.update({
where: { id: user.id },
data: { hashedPassword },
})
}

View File

@@ -0,0 +1,43 @@
import { prisma } from "db"
import { generateToken, hash256 } from "@blitzjs/auth"
import { forgotPasswordMailer } from "mailers/forgotPasswordMailer"
import { ForgotPassword } from "../validations"
import { Ctx } from "@blitzjs/next"
const RESET_PASSWORD_TOKEN_EXPIRATION_IN_HOURS = 4
export default async function forgotPassword(input, ctx: Ctx) {
ForgotPassword.parse(input)
// 1. Get the user
const user = await prisma.user.findFirst({ where: { email: input.email.toLowerCase() } })
// 2. Generate the token and expiration date.
const token = generateToken()
const hashedToken = hash256(token)
const expiresAt = new Date()
expiresAt.setHours(expiresAt.getHours() + RESET_PASSWORD_TOKEN_EXPIRATION_IN_HOURS)
// 3. If user with this email was found
if (user) {
// 4. Delete any existing password reset tokens
await prisma.token.deleteMany({ where: { type: "RESET_PASSWORD", userId: user.id } })
// 5. Save this new token in the database.
await prisma.token.create({
data: {
user: { connect: { id: user.id } },
type: "RESET_PASSWORD",
expiresAt,
hashedToken,
sentTo: user.email,
},
})
// 6. Send the email
await forgotPasswordMailer({ to: user.email, token }).send()
} else {
// 7. If no user found wait the same time so attackers can't tell the difference
await new Promise((resolve) => setTimeout(resolve, 750))
}
// 8. Return the same result whether a password reset email was sent or not
return
}

View File

@@ -0,0 +1,28 @@
import { AuthenticationError } from "blitz"
import { prisma } from "db"
import { Login } from "../validations"
import { SecurePassword } from "@blitzjs/auth"
export const authenticateUser = async (rawEmail: string, rawPassword: string) => {
const { email, password } = Login.parse({ email: rawEmail, password: rawPassword })
const user = await prisma.user.findFirst({ where: { email } })
if (!user) throw new AuthenticationError()
const result = await SecurePassword.verify(user.hashedPassword, password)
if (result === SecurePassword.VALID_NEEDS_REHASH) {
// Upgrade hashed password with a more secure hash
const improvedHash = await SecurePassword.hash(password)
await prisma.user.update({ where: { id: user.id }, data: { hashedPassword: improvedHash } })
}
const { hashedPassword, ...rest } = user
return rest
}
export default async function login(input, ctx) {
const user = await authenticateUser(input.email, input.password)
await ctx.session.$create({ userId: user.id, role: user.role })
return user
}

View File

@@ -0,0 +1,5 @@
import { Ctx } from "blitz"
export default async function logout(_: any, ctx: Ctx) {
return await ctx.session.$revoke()
}

View File

@@ -0,0 +1,48 @@
import { SecurePassword, hash256 } from "@blitzjs/auth"
import { prisma } from "db"
import { ResetPassword } from "../validations"
import login from "./login"
export class ResetPasswordError extends Error {
name = "ResetPasswordError"
message = "Reset password link is invalid or it has expired."
}
export default async function resetPassword(input, ctx) {
ResetPassword.parse(input)
// 1. Try to find this token in the database
const hashedToken = hash256(input.token)
const possibleToken = await prisma.token.findFirst({
where: { hashedToken, type: "RESET_PASSWORD" },
include: { user: true },
})
// 2. If token not found, error
if (!possibleToken) {
throw new ResetPasswordError()
}
const savedToken = possibleToken
// 3. Delete token so it can't be used again
await prisma.token.delete({ where: { id: savedToken.id } })
// 4. If token has expired, error
if (savedToken.expiresAt < new Date()) {
throw new ResetPasswordError()
}
// 5. Since token is valid, now we can update the user's password
const hashedPassword = await SecurePassword.hash(input.password.trim())
const user = await prisma.user.update({
where: { id: savedToken.userId },
data: { hashedPassword },
})
// 6. Revoke all existing login sessions for this user
await prisma.session.deleteMany({ where: { userId: user.id } })
// 7. Now log the user in with the new credentials
await login({ email: user.email, password: input.password }, ctx)
return true
}

View File

@@ -0,0 +1,19 @@
import { prisma } from "db"
import { SecurePassword } from "@blitzjs/auth"
export default async function signup(input, ctx) {
const blitzContext = ctx
const hashedPassword = await SecurePassword.hash((input.password as string) || "test-password")
const email = (input.email as string) || "test" + Math.random() + "@test.com"
const user = await prisma.user.create({
data: { email, hashedPassword, role: "user" },
select: { id: true, name: true, email: true, role: true },
})
await blitzContext.session.$create({
userId: user.id,
})
return { userId: blitzContext.session.userId, ...user, email: input.email }
}

View File

@@ -0,0 +1,42 @@
import { z } from "zod"
export const email = z
.string()
.email()
.transform((str) => str.toLowerCase().trim())
export const password = z
.string()
.min(10)
.max(100)
.transform((str) => str.trim())
export const Signup = z.object({
email,
password,
})
export const Login = z.object({
email,
password: z.string(),
})
export const ForgotPassword = z.object({
email,
})
export const ResetPassword = z
.object({
password: password,
passwordConfirmation: password,
token: z.string(),
})
.refine((data) => data.password === data.passwordConfirmation, {
message: "Passwords don't match",
path: ["passwordConfirmation"], // set the path of the error
})
export const ChangePassword = z.object({
currentPassword: z.string(),
newPassword: password,
})

View File

@@ -0,0 +1,20 @@
import { AuthClientPlugin } from "@blitzjs/auth"
import { setupClient } from "@blitzjs/next"
import { BlitzRpcPlugin } from "@blitzjs/rpc"
const { withBlitz } = setupClient({
plugins: [
AuthClientPlugin({
cookiePrefix: "web-cookie-prefix",
}),
BlitzRpcPlugin({
reactQueryOptions: {
queries: {
staleTime: 7000,
},
},
}),
],
})
export { withBlitz }

View File

@@ -0,0 +1,17 @@
import { setupBlitz } from "@blitzjs/next"
import { AuthServerPlugin, PrismaStorage } from "@blitzjs/auth"
import { prisma as db } from "../db/index"
import { simpleRolesIsAuthorized } from "@blitzjs/auth"
const { gSSP, gSP, api } = setupBlitz({
plugins: [
AuthServerPlugin({
cookiePrefix: "web-cookie-prefix",
// TODO fix type
storage: PrismaStorage(db as any),
isAuthorized: simpleRolesIsAuthorized,
}),
],
})
export { gSSP, gSP, api }

View File

@@ -0,0 +1,83 @@
import { useState, ReactNode, PropsWithoutRef } from "react"
import { FormProvider, useForm, UseFormProps } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import { z } from "zod"
export interface FormProps<S extends z.ZodType<any, any>>
extends Omit<PropsWithoutRef<JSX.IntrinsicElements["form"]>, "onSubmit"> {
/** All your form fields */
children?: ReactNode
/** Text to display in the submit button */
submitText?: string
schema?: S
onSubmit: (values: z.infer<S>) => Promise<void | OnSubmitResult>
initialValues?: UseFormProps<z.infer<S>>["defaultValues"]
}
interface OnSubmitResult {
FORM_ERROR?: string
[prop: string]: any
}
export const FORM_ERROR = "FORM_ERROR"
export function Form<S extends z.ZodType<any, any>>({
children,
submitText,
schema,
initialValues,
onSubmit,
...props
}: FormProps<S>) {
const ctx = useForm<z.infer<S>>({
mode: "onBlur",
resolver: schema ? zodResolver(schema) : undefined,
defaultValues: initialValues,
})
const [formError, setFormError] = useState<string | null>(null)
return (
<FormProvider {...ctx}>
<form
onSubmit={ctx.handleSubmit(async (values) => {
const result = (await onSubmit(values)) || {}
for (const [key, value] of Object.entries(result)) {
if (key === FORM_ERROR) {
setFormError(value)
} else {
ctx.setError(key as any, {
type: "submit",
message: value,
})
}
}
})}
className="form"
{...props}
>
{/* Form fields supplied as children are rendered here */}
{children}
{formError && (
<div role="alert" style={{ color: "red" }}>
{formError}
</div>
)}
{submitText && (
<button type="submit" disabled={ctx.formState.isSubmitting}>
{submitText}
</button>
)}
<style global jsx>{`
.form > * + * {
margin-top: 1rem;
}
`}</style>
</form>
</FormProvider>
)
}
export default Form

View File

@@ -0,0 +1,59 @@
import { forwardRef, PropsWithoutRef, ComponentPropsWithoutRef } from "react"
import { useFormContext } from "react-hook-form"
export interface LabeledTextFieldProps extends PropsWithoutRef<JSX.IntrinsicElements["input"]> {
/** Field name. */
name: string
/** Field label. */
label: string
/** Field type. Doesn't include radio buttons and checkboxes */
type?: "text" | "password" | "email" | "number"
outerProps?: PropsWithoutRef<JSX.IntrinsicElements["div"]>
labelProps?: ComponentPropsWithoutRef<"label">
}
export const LabeledTextField = forwardRef<HTMLInputElement, LabeledTextFieldProps>(
({ label, outerProps, labelProps, name, ...props }, ref) => {
const {
register,
formState: { isSubmitting, errors },
} = useFormContext()
const error = Array.isArray(errors[name])
? errors[name].join(", ")
: errors[name]?.message || errors[name]
return (
<div {...outerProps}>
<label {...labelProps}>
{label}
<input disabled={isSubmitting} {...register(name)} {...props} />
</label>
{error && (
<div role="alert" style={{ color: "red" }}>
{error}
</div>
)}
<style jsx>{`
label {
display: flex;
flex-direction: column;
align-items: start;
font-size: 1rem;
}
input {
font-size: 1rem;
padding: 0.25rem 0.5rem;
border-radius: 3px;
border: 1px solid purple;
appearance: none;
margin-top: 0.5rem;
}
`}</style>
</div>
)
}
)
export default LabeledTextField

View File

@@ -0,0 +1,7 @@
import { useQuery } from "@blitzjs/rpc"
import getCurrentUser from "app/users/queries/getCurrentUser"
export const useCurrentUser = () => {
const [user] = useQuery(getCurrentUser, null)
return user
}

View File

@@ -0,0 +1,17 @@
import Head from "next/head"
import React, { FC } from "react"
const Layout: FC<{ title?: string; children?: React.ReactNode }> = ({ title, children }) => {
return (
<>
<Head>
<title>{title || "__name__"}</title>
<link rel="icon" href="/favicon.ico" />
</Head>
{children}
</>
)
}
export default Layout

View File

@@ -0,0 +1,13 @@
import { Ctx } from "blitz"
import { prisma } from "db"
export default async function getCurrentUser(_ = null, { session }: Ctx) {
if (!session.userId) return null
const user = await prisma.user.findFirst({
where: { id: session.userId as number },
select: { id: true, name: true, email: true, role: true },
})
return user
}

View File

@@ -0,0 +1,8 @@
import { enhancePrisma } from "blitz"
import { PrismaClient } from "@prisma/client"
const EnhancedPrisma = enhancePrisma(PrismaClient)
export * from "@prisma/client"
const prisma = new EnhancedPrisma()
export { prisma }

View File

@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "sqlite"

View File

@@ -0,0 +1,65 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
datasource db {
provider = "sqlite"
url = "file:./db.sqlite"
}
generator client {
provider = "prisma-client-js"
}
// --------------------------------------
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String?
email String @unique
hashedPassword String?
role String @default("USER")
tokens Token[]
sessions Session[]
}
model Session {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
expiresAt DateTime?
handle String @unique
hashedSessionToken String?
antiCSRFToken String?
publicData String?
privateData String?
user User? @relation(fields: [userId], references: [id])
userId Int?
}
model Token {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
hashedToken String
type String
// See note below about TokenType enum
// type TokenType
expiresAt DateTime
sentTo String
user User @relation(fields: [userId], references: [id])
userId Int
@@unique([hashedToken, type])
}
// NOTE: It's highly recommended to use an enum for the token type
// but enums only work in Postgres.
// See: https://blitzjs.com/docs/database-overview#switch-to-postgre-sql
// enum TokenType {
// RESET_PASSWORD
// }

View File

@@ -0,0 +1,15 @@
// import db from "./index"
/*
* This seed function is executed when you run `blitz db seed`.
*
* Probably you want to use a library like https://chancejs.com
* to easily generate realistic data.
*/
const seed = async () => {
// for (let i = 0; i < 5; i++) {
// await db.project.create({ data: { name: "Project " + i } })
// }
}
export default seed

View File

@@ -0,0 +1,56 @@
# dependencies
node_modules
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
.pnp.*
.npm
web_modules/
# blitz
/.blitz/
/.next/
*.sqlite
*.sqlite-journal
.now
.blitz**
blitz-log.log
# misc
.DS_Store
# local env files
.env.local
.env.*.local
.envrc
# Logs
logs
*.log
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Testing
.coverage
*.lcov
.nyc_output
lib-cov
# Caches
*.tsbuildinfo
.eslintcache
.node_repl_history
.yarn-integrity
# Serverless directories
.serverless/
# Stores VSCode versions used for testing VSCode extensions
.vscode-test

View File

View File

@@ -0,0 +1,11 @@
const nextJest = require("@blitzjs/next/jest")
const createJestConfig = nextJest({
dir: "./",
})
const customJestConfig = {
testEnvironment: "jest-environment-jsdom",
}
module.exports = createJestConfig(customJestConfig)

View File

@@ -0,0 +1,5 @@
{
"compilerOptions": {
"baseUrl": "."
}
}

View File

View File

@@ -0,0 +1,45 @@
/* TODO - You need to add a mailer integration in `integrations/` and import here.
*
* The integration file can be very simple. Instantiate the email client
* and then export it. That way you can import here and anywhere else
* and use it straight away.
*/
type ResetPasswordMailer = {
to: string
token: string
}
export function forgotPasswordMailer({ to, token }: ResetPasswordMailer) {
// In production, set APP_ORIGIN to your production server origin
const origin = process.env.APP_ORIGIN || process.env.BLITZ_DEV_SERVER_ORIGIN
const resetUrl = `${origin}/reset-password?token=${token}`
const msg = {
from: "TODO@example.com",
to,
subject: "Your Password Reset Instructions",
html: `
<h1>Reset Your Password</h1>
<h3>NOTE: You must set up a production email integration in mailers/forgotPasswordMailer.ts</h3>
<a href="${resetUrl}">
Click here to set a new password
</a>
`,
}
return {
async send() {
if (process.env.NODE_ENV === "production") {
// TODO - send the production email, like this:
// await postmark.sendEmail(msg)
throw new Error("No production email implementation in mailers/forgotPasswordMailer")
} else {
// Preview email in the browser
const previewEmail = (await import("preview-email")).default
await previewEmail(msg)
}
},
}
}

View File

@@ -1,5 +1,4 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited

View File

@@ -0,0 +1,10 @@
const withBundleAnalyzer = require("@next/bundle-analyzer")({
enabled: process.env.ANALYZE === "true",
})
const { withBlitz } = require("@blitzjs/next")
module.exports = withBlitz(
withBundleAnalyzer({
reactStrictMode: true,
})
)

7
apps/toolkit-app/npmrc Normal file
View File

@@ -0,0 +1,7 @@
save-exact=true
legacy-peer-deps=true
public-hoist-pattern[]=next
public-hoist-pattern[]=secure-password
public-hoist-pattern[]=*jest*
public-hoist-pattern[]=@testing-library/*

View File

@@ -0,0 +1,60 @@
{
"name": "toolkit-app",
"version": "1.0.1-alpha.0",
"scripts": {
"start:dev": "pnpm run prisma:start && next dev",
"buildapp": "prisma generate && next build",
"start": "next start",
"lint": "next lint",
"prisma:start": "prisma generate && prisma migrate deploy",
"prisma:studio": "prisma studio",
"test:local": "jest"
},
"prisma": {
"schema": "db/schema.prisma"
},
"prettier": {
"semi": false,
"printWidth": 100
},
"lint-staged": {
"*.{js}": [
"eslint --fix"
]
},
"dependencies": {
"@blitzjs/auth": "workspace:*",
"@blitzjs/config": "workspace:*",
"@blitzjs/next": "workspace:*",
"@blitzjs/rpc": "workspace:*",
"@hookform/resolvers": "2.8.8",
"@prisma/client": "3.9.0",
"blitz": "workspace:2.0.0-alpha.5",
"next": "12.1.1",
"prisma": "3.9.0",
"react": "18.0.0",
"react-dom": "18.0.0",
"react-hook-form": "7.29.0",
"ts-node": "10.7.0",
"zod": "3.10.1"
},
"devDependencies": {
"@next/bundle-analyzer": "12.0.8",
"@testing-library/react": "13.0.0",
"@testing-library/react-hooks": "7.0.2",
"@types/jest": "27.4.1",
"@types/node": "17.0.16",
"@types/preview-email": "2.0.1",
"@types/react": "17.0.43",
"eslint": "7.32.0",
"husky": "7.0.4",
"jest": "27.5.1",
"lint-staged": "12.1.7",
"prettier": "^2.5.1",
"prettier-plugin-prisma": "3.8.0",
"pretty-quick": "3.1.3",
"preview-email": "3.x",
"typescript": "^4.5.3"
},
"private": true
}

View File

@@ -0,0 +1,37 @@
import { ErrorFallbackProps, ErrorComponent, ErrorBoundary } from "@blitzjs/next"
import { AuthenticationError, AuthorizationError } from "blitz"
import type { AppProps } from "next/app"
import React, { Suspense } from "react"
import { withBlitz } from "app/blitz-client"
function RootErrorFallback({ error }: ErrorFallbackProps) {
if (error instanceof AuthenticationError) {
return <div>Error: You are not authenticated</div>
} else if (error instanceof AuthorizationError) {
return (
<ErrorComponent
statusCode={error.statusCode}
title="Sorry, you are not authorized to access this"
/>
)
} else {
return (
<ErrorComponent
statusCode={(error as any)?.statusCode || 400}
title={error.message || error.name}
/>
)
}
}
function MyApp({ Component, pageProps }: AppProps) {
return (
<ErrorBoundary FallbackComponent={RootErrorFallback}>
<Suspense fallback="Loading...">
<Component {...pageProps} />
</Suspense>
</ErrorBoundary>
)
}
export default withBlitz(MyApp)

View File

@@ -0,0 +1,4 @@
import { rpcHandler } from "@blitzjs/rpc"
import { api } from "app/blitz-server"
export default api(rpcHandler({ onError: console.log }))

View File

@@ -1,15 +1,15 @@
import { BlitzPage, useMutation } from "blitz"
import Layout from "app/core/layouts/Layout"
import { LabeledTextField } from "app/core/components/LabeledTextField"
import { Form, FORM_ERROR } from "app/core/components/Form"
import { ForgotPassword } from "app/auth/validations"
import forgotPassword from "app/auth/mutations/forgotPassword"
import { useMutation } from "@blitzjs/rpc"
const ForgotPasswordPage: BlitzPage = () => {
const ForgotPasswordPage = () => {
const [forgotPasswordMutation, { isSuccess }] = useMutation(forgotPassword)
return (
<div>
<Layout title="Forgot Your Password?">
<h1>Forgot your password?</h1>
{isSuccess ? (
@@ -38,11 +38,8 @@ const ForgotPasswordPage: BlitzPage = () => {
<LabeledTextField name="email" label="Email" placeholder="Email" />
</Form>
)}
</div>
</Layout>
)
}
ForgotPasswordPage.redirectAuthenticatedTo = "/"
ForgotPasswordPage.getLayout = (page) => <Layout title="Forgot Your Password?">{page}</Layout>
export default ForgotPasswordPage

View File

@@ -1,23 +1,20 @@
import { useRouter, BlitzPage } from "blitz"
import Layout from "app/core/layouts/Layout"
import { LoginForm } from "app/auth/components/LoginForm"
import { useRouter } from "next/router"
const LoginPage: BlitzPage = () => {
const LoginPage = () => {
const router = useRouter()
return (
<div>
<Layout title="Log In">
<LoginForm
onSuccess={(_user) => {
const next = router.query.next ? decodeURIComponent(router.query.next as string) : "/"
router.push(next)
return router.push(next)
}}
/>
</div>
</Layout>
)
}
LoginPage.redirectAuthenticatedTo = "/"
LoginPage.getLayout = (page) => <Layout title="Log In">{page}</Layout>
export default LoginPage

View File

@@ -0,0 +1,15 @@
import { useRouter } from "next/router"
import Layout from "app/core/layouts/Layout"
import { SignupForm } from "app/auth/components/SignupForm"
const SignupPage = () => {
const router = useRouter()
return (
<Layout title="Sign Up">
<SignupForm onSuccess={() => router.push("/")} />
</Layout>
)
}
export default SignupPage

View File

@@ -0,0 +1,273 @@
import { Suspense } from "react"
import Image from "next/image"
import Link from "next/link"
import Layout from "app/core/layouts/Layout"
import { useCurrentUser } from "app/core/hooks/useCurrentUser"
import logout from "app/auth/mutations/logout"
import logo from "public/logo.png"
import { useMutation } from "@blitzjs/rpc"
/*
* This file is just for a pleasant getting started page for your new app.
* You can delete everything in here and start from scratch if you like.
*/
const UserInfo = () => {
const currentUser = useCurrentUser()
const [logoutMutation] = useMutation(logout)
if (currentUser) {
return (
<>
<button
className="button small"
onClick={async () => {
await logoutMutation()
}}
>
Logout
</button>
<div>
User id: <code>{currentUser.id}</code>
<br />
User role: <code>{currentUser.role}</code>
</div>
</>
)
} else {
return (
<>
<Link href="/auth/signup" passHref>
<a className="button small">
<strong>Sign Up</strong>
</a>
</Link>
<Link href="/auth/login" passHref>
<a className="button small">
<strong>Login</strong>
</a>
</Link>
</>
)
}
}
const Home = () => {
return (
<Layout title="Home">
<div className="container">
<main>
<div className="logo">
<Image src={`${logo.src}`} alt="blitzjs" width="256px" height="118px" layout="fixed" />
</div>
<p>
<strong>Congrats!</strong> Your app is ready, including user sign-up and log-in.
</p>
<div className="buttons" style={{ marginTop: "1rem", marginBottom: "1rem" }}>
<Suspense fallback="Loading...">
<UserInfo />
</Suspense>
</div>
<p>
<strong>
To add a new model to your app, <br />
run the following in your terminal:
</strong>
</p>
<pre>
<code>blitz generate all project name:string</code>
</pre>
<div style={{ marginBottom: "1rem" }}>(And select Yes to run prisma migrate)</div>
<div>
<p>
Then <strong>restart the server</strong>
</p>
<pre>
<code>Ctrl + c</code>
</pre>
<pre>
<code>blitz dev</code>
</pre>
<p>
and go to{" "}
<Link href="/projects">
<a>/projects</a>
</Link>
</p>
</div>
<div className="buttons" style={{ marginTop: "5rem" }}>
<a
className="button"
href="https://blitzjs.com/docs/getting-started?utm_source=blitz-new&utm_medium=app-template&utm_campaign=blitz-new"
target="_blank"
rel="noopener noreferrer"
>
Documentation
</a>
<a
className="button-outline"
href="https://github.com/blitz-js/blitz"
target="_blank"
rel="noopener noreferrer"
>
Github Repo
</a>
<a
className="button-outline"
href="https://discord.blitzjs.com"
target="_blank"
rel="noopener noreferrer"
>
Discord Community
</a>
</div>
</main>
<footer>
<a
href="https://blitzjs.com?utm_source=blitz-new&utm_medium=app-template&utm_campaign=blitz-new"
target="_blank"
rel="noopener noreferrer"
>
Powered by Blitz.js
</a>
</footer>
<style jsx global>{`
@import url("https://fonts.googleapis.com/css2?family=Libre+Franklin:wght@300;700&display=swap");
html,
body {
padding: 0;
margin: 0;
font-family: "Libre Franklin", -apple-system, BlinkMacSystemFont, Segoe UI, Roboto,
Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
}
* {
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
box-sizing: border-box;
}
.container {
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
main {
padding: 5rem 0;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
main p {
font-size: 1.2rem;
}
p {
text-align: center;
}
footer {
width: 100%;
height: 60px;
border-top: 1px solid #eaeaea;
display: flex;
justify-content: center;
align-items: center;
background-color: #45009d;
}
footer a {
display: flex;
justify-content: center;
align-items: center;
}
footer a {
color: #f4f4f4;
text-decoration: none;
}
.logo {
margin-bottom: 2rem;
}
.logo img {
width: 300px;
}
.buttons {
display: grid;
grid-auto-flow: column;
grid-gap: 0.5rem;
}
.button {
font-size: 1rem;
background-color: #6700eb;
padding: 1rem 2rem;
color: #f4f4f4;
text-align: center;
}
.button.small {
padding: 0.5rem 1rem;
}
.button:hover {
background-color: #45009d;
}
.button-outline {
border: 2px solid #6700eb;
padding: 1rem 2rem;
color: #6700eb;
text-align: center;
}
.button-outline:hover {
border-color: #45009d;
color: #45009d;
}
pre {
background: #fafafa;
border-radius: 5px;
padding: 0.75rem;
text-align: center;
}
code {
font-size: 0.9rem;
font-family: Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono,
Bitstream Vera Sans Mono, Courier New, monospace;
}
.grid {
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
max-width: 800px;
margin-top: 3rem;
}
@media (max-width: 600px) {
.grid {
width: 100%;
flex-direction: column;
}
}
`}</style>
</div>
</Layout>
)
}
export default Home

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

View File

@@ -0,0 +1,4 @@
// This is the jest 'setupFilesAfterEnv' setup file
// It's a good place to set globals, add global before/after hooks, etc
export {} // so TS doesn't complain

View File

@@ -0,0 +1,24 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"baseUrl": "./",
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"strictNullChecks": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"noUncheckedIndexedAccess": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"tsBuildInfoFile": ".tsbuildinfo"
},
"exclude": ["node_modules", "**/*.e2e.ts", "cypress"],
"include": ["blitz-env.d.ts", "**/*.ts", "**/*.tsx"]
}

View File

@@ -17,7 +17,8 @@
"test": "turbo run test",
"clean": "turbo run clean && rm -rf node_modules",
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
"release": "changeset publish && git push --follow-tags"
"pre-publish": "pnpm i && pnpm build && changeset add && changeset version && git add . && git commit -v",
"publish-release": "changeset publish && git push --follow-tags"
},
"dependencies": {
"@blitzjs/manypkg": "0.19.1",

View File

@@ -1,5 +1,27 @@
# @blitzjs/auth
## 2.0.0-alpha.5
### Patch Changes
- new app template
- Updated dependencies
- blitz@2.0.0-alpha.5
## 2.0.0-alpha.4
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.4
## 2.0.0-alpha.3
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.3
## 2.0.0-alpha.2
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/auth",
"version": "2.0.0-alpha.2",
"version": "2.0.0-alpha.5",
"scripts": {
"build": "unbuild",
"predev": "wait-on -d 250 ../blitz/dist/index-server.d.ts",

View File

@@ -1,5 +1,25 @@
# @blitzjs/next
## 2.0.0-alpha.5
### Patch Changes
- new app template
- Updated dependencies
- @blitzjs/rpc@2.0.0-alpha.5
## 2.0.0-alpha.4
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.4
## 2.0.0-alpha.3
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.3
## 2.0.0-alpha.2
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/next",
"version": "2.0.0-alpha.2",
"version": "2.0.0-alpha.5",
"scripts": {
"build": "unbuild",
"dev": "pnpm predev && pnpm watch unbuild src --wait=0.2",
@@ -20,7 +20,7 @@
"dist/**"
],
"dependencies": {
"@blitzjs/rpc": "2.0.0-alpha.2",
"@blitzjs/rpc": "2.0.0-alpha.5",
"debug": "4.3.3",
"fs-extra": "10.0.1",
"react-query": "3.21.1"

View File

@@ -1,5 +1,30 @@
# @blitzjs/rpc
## 2.0.0-alpha.5
### Patch Changes
- new app template
- Updated dependencies
- blitz@2.0.0-alpha.5
- @blitzjs/auth@2.0.0-alpha.5
## 2.0.0-alpha.4
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.4
- @blitzjs/auth@2.0.0-alpha.4
## 2.0.0-alpha.3
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.3
- @blitzjs/auth@2.0.0-alpha.3
## 2.0.0-alpha.2
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/rpc",
"version": "2.0.0-alpha.2",
"version": "2.0.0-alpha.5",
"scripts": {
"build": "unbuild",
"predev": "wait-on -d 250 ../blitz/dist/index-server.d.ts && wait-on -d 250 ../blitz-auth/dist/index-browser.d.ts",
@@ -20,7 +20,7 @@
"dist/**"
],
"dependencies": {
"@blitzjs/auth": "2.0.0-alpha.2",
"@blitzjs/auth": "2.0.0-alpha.5",
"b64-lite": "1.4.0",
"bad-behavior": "1.0.1",
"chalk": "^4.1.0",
@@ -33,7 +33,7 @@
"@types/debug": "4.1.7",
"@types/react": "17.0.43",
"@types/react-dom": "17.0.14",
"blitz": "2.0.0-alpha.2",
"blitz": "2.0.0-alpha.5",
"next": "12.1.1",
"react": "18.0.0",
"react-dom": "18.0.0",
@@ -42,7 +42,7 @@
"watch": "1.0.2"
},
"peerDependencies": {
"blitz": "2.0.0-alpha.2",
"blitz": "2.0.0-alpha.5",
"next": "*"
},
"publishConfig": {

View File

@@ -1,5 +1,27 @@
# blitz
## 2.0.0-alpha.5
### Patch Changes
- new app template
- Updated dependencies
- @blitzjs/generator@2.0.0-alpha.5
## 2.0.0-alpha.4
### Patch Changes
- fix more cli problems
- @blitzjs/generator@2.0.0-alpha.4
## 2.0.0-alpha.3
### Patch Changes
- downgrade pkg-dir to non-esm only version
- @blitzjs/generator@2.0.0-alpha.3
## 2.0.0-alpha.2
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "blitz",
"version": "2.0.0-alpha.2",
"version": "2.0.0-alpha.5",
"scripts": {
"build": "unbuild",
"dev": "watch unbuild src --wait=0.2",
@@ -38,7 +38,7 @@
"npm-which": "3.0.1",
"ora": "5.3.0",
"p-event": "4.2.0",
"pkg-dir": "6.0.1",
"pkg-dir": "5.0.0",
"prompts": "2.4.2",
"resolve-cwd": "3.0.0",
"superjson": "1.8.0",

View File

@@ -19,12 +19,7 @@ import prompts from "prompts"
import chalk from "chalk"
const getIsTypeScript = async () =>
require("fs").existsSync(
require("path").join(
await require("next/dist/server/lib/utils").getProjectRoot(process.cwd()),
"tsconfig.json",
),
)
require("fs").existsSync(require("path").join(process.cwd(), "tsconfig.json"))
enum ResourceType {
All = "all",

View File

@@ -202,7 +202,7 @@ const determinePkgManagerToInstallDeps = async () => {
if (res.pkgManager === "skip") {
shouldInstallDeps = false
} else {
shouldInstallDeps = res.pkgManager
shouldInstallDeps = true
}
} else {
const res = await prompts({
@@ -261,6 +261,7 @@ const newApp: CliCommand = async (argv) => {
{ignoreCache: true},
)
const result = await runPrisma(["migrate", "dev", "--name", "Initial migration"], true)
console.log("primsa result", result)
if (!result.success) throw new Error()
} catch (error) {
postInstallSteps.push(

View File

@@ -56,12 +56,15 @@ if (!foundCommand && args["--help"]) {
console.log(`
Usage
$ blitz <command>
Available commands
${Object.keys(commands).join(", ")}
Options
--env, -e App environment name
--version, -v Version number
--help, -h Displays this message
For more information run a command with the --help flag
$ blitz build --help
`)
@@ -71,20 +74,6 @@ if (!foundCommand && args["--help"]) {
const command = foundCommand ? (args._[0] as string) : defaultCommand
const forwardedArgs = foundCommand ? args._.slice(1) : args._
// Don't check for react or react-dom when running blitz new
if (command !== "new") {
;["react", "react-dom"].forEach((dependency) => {
try {
// When 'npm link' is used it checks the clone location. Not the project.
require.resolve(dependency)
} catch (err) {
console.warn(
`The module '${dependency}' was not found. Blitz.js requires that you include it in 'dependencies' of your 'package.json'. To add it, run 'npm install ${dependency}'`,
)
}
})
}
if (args["--help"]) {
forwardedArgs.push("--help")
}
@@ -117,21 +106,3 @@ commands[command]?.()
.catch((err) => {
console.log(err)
})
if (command === "dev") {
const {watchFile} = require("fs")
watchFile(`${process.cwd()}/blitz.config.js`, (cur: any, prev: any) => {
if (cur.size > 0 || prev.size > 0) {
console.log(
`\n> Found a change in blitz.config.js. Restart the server to see the changes in effect.`,
)
}
})
watchFile(`${process.cwd()}/blitz.config.ts`, (cur: any, prev: any) => {
if (cur.size > 0 || prev.size > 0) {
console.log(
`\n> Found a change in blitz.config.ts. Restart the server to see the changes in effect.`,
)
}
})
}

View File

@@ -2,12 +2,12 @@ import fs, {promises} from "fs"
import {join, resolve} from "path"
import {readJSON} from "fs-extra"
import path from "path"
import {packageDirectory} from "pkg-dir"
import pkgDir from "pkg-dir"
import resolveCwd from "resolve-cwd"
const debug = require("debug")("blitz:utils")
export async function resolveBinAsync(pkg: string, executable = pkg) {
const packageDir = await packageDirectory({cwd: resolveCwd(pkg)})
const packageDir = await pkgDir(resolveCwd(pkg))
if (!packageDir) throw new Error(`Could not find package.json for '${pkg}'`)
const {bin} = await readJSON(path.join(packageDir, "package.json"))

View File

@@ -4,7 +4,7 @@ import detect from "detect-port"
import path from "path"
import {existsSync, readJSONSync} from "fs-extra"
import * as esbuild from "esbuild"
import {packageDirectorySync} from "pkg-dir"
import pkgDir from "pkg-dir"
import type {ServerConfig} from "./config"
const debug = require("debug")("blitz:utils")
@@ -50,7 +50,7 @@ export function getCustomServerBuildPath() {
}
const getEsbuildOptions = (): esbuild.BuildOptions => {
const pkg = readJSONSync(path.join(packageDirectorySync()!, "package.json"))
const pkg = readJSONSync(path.join(pkgDir.sync()!, "package.json"))
return {
entryPoints: [getCustomServerPath()],
outfile: getCustomServerBuildPath(),

View File

@@ -1,5 +1,15 @@
# @blitzjs/config
## 2.0.0-alpha.5
### Patch Changes
- new app template
## 2.0.0-alpha.4
## 2.0.0-alpha.3
## 2.0.0-alpha.2
## 2.0.0-alpha.1

View File

@@ -1,7 +1,7 @@
{
"name": "@blitzjs/config",
"private": true,
"version": "2.0.0-alpha.2",
"version": "2.0.0-alpha.5",
"license": "MIT",
"dependencies": {
"@typescript-eslint/eslint-plugin": "5.9.1",

View File

@@ -1,5 +1,15 @@
# @blitzjs/generator
## 2.0.0-alpha.5
### Patch Changes
- new app template
## 2.0.0-alpha.4
## 2.0.0-alpha.3
## 2.0.0-alpha.2
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/generator",
"version": "2.0.0-alpha.2",
"version": "2.0.0-alpha.5",
"scripts": {
"dev": "watch unbuild src --wait=0.2",
"build": "unbuild && pnpm build:templates",
@@ -26,6 +26,7 @@
"@babel/types": "7.12.10",
"@mrleebo/prisma-ast": "0.2.6",
"chalk": "^4.1.0",
"console-table-printer": "2.10.0",
"cross-spawn": "7.0.3",
"diff": "5.0.0",
"enquirer": "2.3.6",
@@ -35,9 +36,11 @@
"mem-fs": "1.2.0",
"mem-fs-editor": "8.0.0",
"npm-which": "3.0.1",
"ora": "5.3.0",
"pluralize": "8.0.0",
"prettier": "^2.5.1",
"recast": "0.20.5",
"tslog": "3.3.1",
"username": "5.1.0",
"vinyl": "2.2.1"
},
@@ -59,6 +62,7 @@
"@typescript-eslint/parser": "5.9.1",
"babylon": "6.18.0",
"debug": "4.3.3",
"eslint": "7.32.0",
"react": "18.0.0",
"typescript": "^4.5.3",
"unbuild": "0.6.9",

View File

@@ -13,6 +13,7 @@ import * as babelParser from "recast/parsers/babel"
import {ConflictChecker} from "./conflict-checker"
import {pipe} from "./utils/pipe"
import {readdirRecursive} from "./utils/readdir-recursive"
import prettier from "prettier"
const debug = require("debug")("blitz:generator")
export const customTsParser = {
@@ -152,6 +153,7 @@ export abstract class Generator<
this.store = createStore()
this.fs = createEditor(this.store)
this.enquirer = new Enquirer()
this.prettier = prettier
this.useTs =
typeof this.options.useTs === "undefined"
? fs.existsSync(path.resolve("tsconfig.json"))
@@ -207,6 +209,7 @@ export abstract class Generator<
}
const inputStr = input.toString("utf-8")
let templatedFile = inputStr
if (codeFileExtensions.test(pathEnding)) {
templatedFile = this.replaceConditionals(inputStr, templateValues, prettierOptions || {})
}
@@ -247,9 +250,7 @@ export abstract class Generator<
const additionalFilesToIgnore = this.filesToIgnore()
return ![...alwaysIgnoreFiles, ...additionalFilesToIgnore].includes(name)
})
try {
this.prettier = await import("prettier")
} catch {}
const prettierOptions = await this.prettier?.resolveConfig(sourcePath)
for (let filePath of paths) {
@@ -258,11 +259,21 @@ export abstract class Generator<
pathSuffix = path.join(this.getTargetDirectory(), pathSuffix)
const templateValues = await this.getTemplateValues()
this.fs.copy(this.sourcePath(filePath), this.destinationPath(pathSuffix), {
process: (input) =>
this.process(input, pathSuffix, templateValues, prettierOptions ?? undefined),
})
let templatedPathSuffix = this.replaceTemplateValues(pathSuffix, templateValues)
const newContent = this.process(
this.fs.read(this.sourcePath(filePath), {raw: true}) as any,
pathSuffix,
templateValues,
prettierOptions ?? undefined,
)
this.fs.write(this.destinationPath(pathSuffix), newContent)
// this.fs.copy(this.sourcePath(filePath), this.destinationPath(pathSuffix), {
// process: (input) =>
// // this.process(input, pathSuffix, templateValues, prettierOptions ?? undefined),
// this.process(input, pathSuffix, templateValues, undefined),
// })
if (!this.useTs && tsExtension.test(this.destinationPath(pathSuffix))) {
templatedPathSuffix = templatedPathSuffix.replace(tsExtension, ".js")
}
@@ -295,6 +306,7 @@ export abstract class Generator<
} else {
return path.join(
__dirname,
"..",
process.env.NODE_ENV === "test" ? "../templates" : "./templates",
this.sourceRoot.path,
...paths,

View File

@@ -1,10 +1,12 @@
import spawn from "cross-spawn"
import chalk from "chalk"
import {readJSONSync, writeJson} from "fs-extra"
import {join} from "path"
import username from "username"
import {Generator, GeneratorOptions, SourceRootType} from "../generator"
import {fetchLatestVersionsFor} from "../utils/fetch-latest-version-for"
import {getBlitzDependencyVersion} from "../utils/get-blitz-dependency-version"
import {baseLogger, log} from "../utils/log"
function assert(condition: any, message: string): asserts condition {
if (!condition) throw new Error(message)
@@ -84,22 +86,23 @@ export class AppGenerator extends Generator<AppGeneratorOptions> {
const {pkgManager} = this
let gitInitSuccessful
if (!this.options.skipGit) {
console.log({spawn})
const initResult = spawn.sync("git", ["init"], {
stdio: "ignore",
})
gitInitSuccessful = initResult.status === 0
if (!gitInitSuccessful) {
console.warn("Failed to run git init.")
console.warn("Find out more about how to install git here: https://git-scm.com/downloads.")
baseLogger({displayDateTime: false}).warn("Failed to run git init.")
baseLogger({displayDateTime: false}).warn(
"Find out more about how to install git here: https://git-scm.com/downloads.",
)
}
}
const pkgJsonLocation = join(this.destinationPath(), "package.json")
const pkg = readJSONSync(pkgJsonLocation)
console.log("") // New line needed
// todo const spinner = log.spinner(log.withBrand("Retrieving the freshest of dependencies")).start()
const spinner = log.spinner(log.withBrand("Retrieving the freshest of dependencies")).start()
const [
{value: newDependencies, isFallback: dependenciesUsedFallback},
@@ -121,7 +124,7 @@ export class AppGenerator extends Generator<AppGeneratorOptions> {
await writeJson(pkgJsonLocation, pkg, {spaces: 2})
if (!fallbackUsed && !this.options.skipInstall) {
// spinner.succeed()
spinner.succeed()
await new Promise<void>((resolve) => {
const logFlag = pkgManager === "yarn" ? "--json" : "--loglevel=error"
@@ -140,10 +143,10 @@ export class AppGenerator extends Generator<AppGeneratorOptions> {
const spinners: any[] = []
if (pkgManager !== "yarn") {
// const spinner = log
// .spinner(log.withBrand("Installing those dependencies (this will take a few minutes)"))
// .start()
// spinners.push(spinner)
const spinner = log
.spinner(log.withBrand("Installing those dependencies (this will take a few minutes)"))
.start()
spinners.push(spinner)
}
cp.stdout?.setEncoding("utf8")
@@ -153,8 +156,8 @@ export class AppGenerator extends Generator<AppGeneratorOptions> {
let json = getJSON(data)
if (json && json.type === "step") {
spinners[spinners.length - 1]?.succeed()
// const spinner = log.spinner(log.withBrand(json.data.message)).start()
// spinners.push(spinner)
const spinner = log.spinner(log.withBrand(json.data.message)).start()
spinners.push(spinner)
}
if (json && json.type === "success") {
spinners[spinners.length - 1]?.succeed()
@@ -195,7 +198,7 @@ export class AppGenerator extends Generator<AppGeneratorOptions> {
if (pkgManager === "yarn") {
return spawn.sync("yarn", ["run", ...command.split(" ")])
} else if (pkgManager === "pnpm") {
return spawn.sync("pnpx", command.split(" "))
return spawn.sync("pnpm", command.split(" "))
} else {
return spawn.sync("npx", command.split(" "))
}
@@ -203,28 +206,28 @@ export class AppGenerator extends Generator<AppGeneratorOptions> {
// Ensure the generated files are formatted with the installed prettier version
if (this.packageInstallSuccess) {
// const formattingSpinner = log.spinner(log.withBrand("Formatting your code")).start()
const formattingSpinner = log.spinner(log.withBrand("Formatting your code")).start()
const prettierResult = runLocalNodeCLI("prettier --loglevel silent --write .")
if (prettierResult.status !== 0) {
// formattingSpinner.fail(
// chalk.yellow.bold(
// "We had an error running Prettier, but don't worry your app will still run fine :)",
// ),
// )
formattingSpinner.fail(
chalk.yellow.bold(
"We had an error running Prettier, but don't worry your app will still run fine :)",
),
)
} else {
// formattingSpinner.succeed()
formattingSpinner.succeed()
}
}
} else {
console.log("") // New line needed
if (this.options.skipInstall) {
// spinner.succeed()
spinner.succeed()
} else {
// spinner.fail(
// chalk.red.bold(
// `We had some trouble connecting to the network, so we'll skip installing your dependencies right now. Make sure to run ${`${this.pkgManager} install`} once you're connected again.`,
// ),
// )
spinner.fail(
chalk.red.bold(
`We had some trouble connecting to the network, so we'll skip installing your dependencies right now. Make sure to run ${`${this.pkgManager} install`} once you're connected again.`,
),
)
}
}
@@ -242,7 +245,7 @@ export class AppGenerator extends Generator<AppGeneratorOptions> {
}
commitChanges() {
// const commitSpinner = log.spinner(log.withBrand("Committing your app")).start()
const commitSpinner = log.spinner(log.withBrand("Committing your app")).start()
const commands: Array<[string, string[], object]> = [
["git", ["add", "."], {stdio: "ignore"}],
[
@@ -264,15 +267,15 @@ export class AppGenerator extends Generator<AppGeneratorOptions> {
for (let command of commands) {
const result = spawn.sync(...command)
if (result.status !== 0) {
// commitSpinner.fail(
// chalk.red.bold(
// `Failed to run command ${command[0]} with ${command[1].join(" ")} options.`,
// ),
// )
commitSpinner.fail(
chalk.red.bold(
`Failed to run command ${command[0]} with ${command[1].join(" ")} options.`,
),
)
return
}
}
// commitSpinner.succeed()
commitSpinner.succeed()
}
private updateForms() {
const pkg = this.fs.readJSON(this.destinationPath("package.json")) as

View File

@@ -19,7 +19,7 @@ export class PageGenerator extends Generator<PageGeneratorOptions> {
super(options)
this.sourceRoot = getTemplateRoot(options.templateDir, {type: "template", path: "page"})
}
static subdirectory = "pages"
static subdirectory = "../../.."
private getId(input: string = "") {
if (!input) return input
@@ -63,7 +63,7 @@ export class PageGenerator extends Generator<PageGeneratorOptions> {
const parent = this.options.parentModels
? `${this.options.parentModels}/__parentModelParam__/`
: ""
return `app/pages/${parent}${kebabCaseModelName}`
return `pages/${parent}${kebabCaseModelName}`
}
async postWrite() {

View File

@@ -0,0 +1,101 @@
import os from "os"
export const VALID_LOADERS = ["default", "imgix", "cloudinary", "akamai", "custom"] as const
export type LoaderValue = typeof VALID_LOADERS[number]
export type ImageConfig = {
deviceSizes: number[]
imageSizes: number[]
loader: LoaderValue
path: string
domains?: string[]
disableStaticImages?: boolean
minimumCacheTTL?: number
}
export const imageConfigDefault: ImageConfig = {
deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
path: "/_next/image",
loader: "default",
domains: [],
disableStaticImages: false,
minimumCacheTTL: 60,
}
export const defaultConfig: {[key: string]: any} = {
env: {},
webpack: null,
webpackDevMiddleware: null,
distDir: ".next",
cleanDistDir: true,
assetPrefix: "",
configOrigin: "default",
useFileSystemPublicRoutes: true,
generateBuildId: () => null,
generateEtags: true,
pageExtensions: ["tsx", "ts", "jsx", "js"],
target: "server",
poweredByHeader: true,
compress: true,
analyticsId: process.env.VERCEL_ANALYTICS_ID || "",
images: imageConfigDefault,
devIndicators: {
buildActivity: true,
},
onDemandEntries: {
maxInactiveAge: 60 * 1000,
pagesBufferLength: 2,
},
amp: {
canonicalBase: "",
},
basePath: "",
sassOptions: {},
trailingSlash: false,
i18n: null,
productionBrowserSourceMaps: false,
optimizeFonts: true,
log: {
level: "info",
},
webpack5: Number(process.env.NEXT_PRIVATE_TEST_WEBPACK4_MODE) > 0 ? false : undefined,
excludeDefaultMomentLocales: true,
serverRuntimeConfig: {},
publicRuntimeConfig: {},
reactStrictMode: false,
httpAgentOptions: {
keepAlive: true,
},
experimental: {
swcLoader: false,
swcMinify: false,
cpus: Math.max(
1,
(Number(process.env.CIRCLE_NODE_TOTAL) || (os.cpus() || {length: 1}).length) - 1,
),
plugins: false,
profiling: false,
isrFlushToDisk: true,
workerThreads: false,
pageEnv: false,
optimizeImages: false,
optimizeCss: false,
scrollRestoration: false,
stats: false,
externalDir: false,
disableOptimizedLoading: false,
gzipSize: true,
craCompat: false,
esmExternals: false,
staticPageGenerationTimeout: 60,
pageDataCollectionTimeout: 60,
// default to 50MB limit
isrMemoryCacheSize: 50 * 1024 * 1024,
concurrentFeatures: false,
},
future: {
strictPostcssConfiguration: false,
},
}

View File

@@ -0,0 +1,254 @@
import {ISettingsParam, Logger} from "tslog"
import c from "chalk"
import {Table} from "console-table-printer"
import ora from "ora"
import readline from "readline"
import {join} from "path"
import {defaultConfig} from "./default-config"
// eslint-disable-next-line
declare module globalThis {
let _blitz_baseLogger: Logger
let _blitz_logLevel: LogLevel
}
export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal"
export function normalizeConfig(phase: string, config: any) {
if (typeof config === "function") {
config = config(phase, {defaultConfig})
if (typeof config.then === "function") {
throw new Error(
"> Promise returned in blitz config. https://nextjs.org/docs/messages/promise-in-next-config",
)
}
}
return config
}
export function loadConfigAtRuntime() {
if (!process.env.BLITZ_APP_DIR) {
throw new Error("Internal Blitz Error: process.env.BLITZ_APP_DIR is not set")
}
return loadConfigProduction(process.env.BLITZ_APP_DIR)
}
export function assignDefaultsBase(userConfig: {[key: string]: any}) {
const config = Object.keys(userConfig).reduce<{[key: string]: any}>((currentConfig, key) => {
const value = userConfig[key]
if (value === undefined || value === null) {
return currentConfig
}
// Copied from assignDefaults in server/config.ts
if (!!value && value.constructor === Object) {
currentConfig[key] = {
...defaultConfig[key],
...Object.keys(value).reduce<any>((c, k) => {
const v = value[k]
if (v !== undefined && v !== null) {
c[k] = v
}
return c
}, {}),
}
} else {
currentConfig[key] = value
}
return currentConfig
}, {})
const result = {...defaultConfig, ...config}
return result
}
export function loadConfigProduction(pagesDir: string) {
let userConfigModule
try {
const path = join(pagesDir, "next.confi.js")
debug("Loading config from ", path)
// eslint-disable-next-line no-eval -- block webpack from following this module path
userConfigModule = eval("require")(path)
} catch {
debug("Did not find custom config file")
// In case user does not have custom config
userConfigModule = {}
}
let userConfig = normalizeConfig(
"phase-production-server",
userConfigModule.default || userConfigModule,
)
return assignDefaultsBase(userConfig) as any
}
export const newline = () => {
globalThis._blitz_logLevel = globalThis._blitz_logLevel ?? loadConfigAtRuntime().log?.level
const logLevel = globalThis._blitz_logLevel
switch (logLevel) {
case "trace":
case "debug":
case "info":
console.log(" ")
break
case "warn":
case "error":
case "fatal":
default:
//nothing
break
}
}
export const baseLogger = (options?: ISettingsParam): Logger => {
if (globalThis._blitz_baseLogger) return globalThis._blitz_baseLogger
let config
try {
config = loadConfigAtRuntime()
} catch {
config = {}
}
globalThis._blitz_baseLogger = new Logger({
minLevel: config.log?.level || "info",
type: config.log?.type || "pretty",
dateTimePattern:
process.env.NODE_ENV === "production"
? "year-month-day hour:minute:second.millisecond"
: "hour:minute:second.millisecond",
displayFunctionName: false,
displayFilePath: "hidden",
displayRequestId: false,
dateTimeTimezone:
process.env.NODE_ENV === "production"
? "utc"
: Intl.DateTimeFormat().resolvedOptions().timeZone,
prettyInspectHighlightStyles: {
name: "yellow",
number: "blue",
bigint: "blue",
boolean: "blue",
},
colorizePrettyLogs: process.env.FORCE_COLOR === "0" ? false : true,
maskValuesOfKeys: ["password", "passwordConfirmation"],
exposeErrorCodeFrame: process.env.NODE_ENV !== "production",
...options,
})
return globalThis._blitz_baseLogger
}
export const table = Table
export const chalk = c
// const blitzTrueBrandColor = '6700AB'
const blitzBrightBrandColor = "8a3df0"
// Using bright brand color so it's better for dark terminals
const brandColor = blitzBrightBrandColor
const withBrand = (str: string) => {
return c.hex(brandColor).bold(str)
}
const withCaret = (str: string) => {
return `${c.gray(">")} ${str}`
}
const withCheck = (str: string) => {
return `${c.green("✔")} ${str}`
}
const withProgress = (str: string) => {
return withCaret(str)
}
/**
* Logs a branded purple message to stdout.
*
* @param {string} msg
*/
const branded = (msg: string) => {
console.log(c.hex(brandColor).bold(msg))
}
/**
* Clears the line and optionally log a message to stdout.
*
* @param {string} msg
*/
const clearLine = (msg?: string) => {
readline.clearLine(process.stdout, 0)
readline.cursorTo(process.stdout, 0)
msg && process.stdout.write(msg)
}
const clearConsole = () => {
if (process.platform === "win32") {
process.stdout.write("\x1B[2J\x1B[0f")
} else {
process.stdout.write("\x1B[2J\x1B[3J\x1B[H")
}
}
/**
* Logs a progress message to stdout.
*
* @param {string} msg
*/
const progress = (msg: string) => {
console.log(withProgress(msg))
}
const spinner = (str: string) => {
return ora({
text: str,
color: "blue",
spinner: {
interval: 120,
frames: ["◢", "◣", "◤", "◥"],
},
})
}
/**
* Logs a green success message to stdout.
*
* @param {string} msg
*/
const success = (msg: string) => {
console.log(withCheck(c.green(msg)))
}
/**
* Colorizes a variable for display.
*
* @param {string} val
*/
const variable = (val: any) => {
return c.cyan.bold(`${val}`)
}
/**
* If the DEBUG env var is set this will write to the console
* @param str msg
*/
const debug = require("debug")("blitz")
export const log = {
withBrand,
withCaret,
branded,
clearLine,
clearConsole,
progress,
spinner,
success,
variable,
debug,
Table,
}

View File

@@ -1,8 +0,0 @@
# This env file should NOT be checked into source control
# This is the place for values that changed for every developer
# SQLite is ready to go out of the box, but you can switch to Postgres
# by first changing the provider from "sqlite" to "postgres" in the Prisma
# schema file and by second swapping the DATABASE_URL below.
DATABASE_URL="file:./db.sqlite"
# DATABASE_URL=postgresql://__username__@localhost:5432/__name__

View File

@@ -1,7 +0,0 @@
# THIS FILE SHOULD NOT BE CHECKED INTO YOUR VERSION CONTROL SYSTEM
# SQLite is ready to go out of the box, but you can switch to Postgres
# by first changing the provider from "sqlite" to "postgres" in the Prisma
# schema file and by second swapping the DATABASE_URL below.
DATABASE_URL="file:./db_test.sqlite"
# DATABASE_URL=postgresql://__username__@localhost:5432/__name___test

View File

@@ -1,3 +1 @@
module.exports = {
extends: ["blitz"],
}
module.exports = require("@blitzjs/config/eslint")

View File

@@ -4,8 +4,6 @@
*.lock
db/migrations
.next
.blitz
.yarn
.pnp.*
node_modules
.blitz.config.compiled.js

View File

@@ -1,8 +1,10 @@
TODO
[![Blitz.js](https://raw.githubusercontent.com/blitz-js/art/master/github-cover-photo.png)](https://blitzjs.com)
This is a [Blitz.js](https://github.com/blitz-js/blitz) app.
# **__name__**
# ****name****
## Getting Started

View File

@@ -1,8 +1,10 @@
import { AuthenticationError, Link, useMutation, Routes, PromiseReturnType } from "blitz"
import { AuthenticationError, PromiseReturnType } from "blitz"
import Link from "next/link"
import { LabeledTextField } from "app/core/components/LabeledTextField"
import { Form, FORM_ERROR } from "app/core/components/Form"
import login from "app/auth/mutations/login"
import { Login } from "app/auth/validations"
import { useMutation } from "@blitzjs/rpc"
type LoginFormProps = {
onSuccess?: (user: PromiseReturnType<typeof login>) => void
@@ -10,7 +12,6 @@ type LoginFormProps = {
export const LoginForm = (props: LoginFormProps) => {
const [loginMutation] = useMutation(login)
return (
<div>
<h1>Login</h1>
@@ -38,14 +39,17 @@ export const LoginForm = (props: LoginFormProps) => {
<LabeledTextField name="email" label="Email" placeholder="Email" />
<LabeledTextField name="password" label="Password" placeholder="Password" type="password" />
<div>
<Link href={Routes.ForgotPasswordPage()}>
<Link href="/auth/forgot-password" passHref>
<a>Forgot your password?</a>
</Link>
</div>
</Form>
<div style={{ marginTop: "1rem" }}>
Or <Link href={Routes.SignupPage()}>Sign Up</Link>
Or{" "}
<Link href="/auth/signup" passHref>
<a>Sign Up</a>
</Link>
</div>
</div>
)

View File

@@ -1,8 +1,8 @@
import { useMutation } from "blitz"
import { LabeledTextField } from "app/core/components/LabeledTextField"
import { Form, FORM_ERROR } from "app/core/components/Form"
import signup from "app/auth/mutations/signup"
import { Signup } from "app/auth/validations"
import { useMutation } from "@blitzjs/rpc"
type SignupFormProps = {
onSuccess?: () => void
@@ -10,7 +10,6 @@ type SignupFormProps = {
export const SignupForm = (props: SignupFormProps) => {
const [signupMutation] = useMutation(signup)
return (
<div>
<h1>Create an Account</h1>

View File

@@ -1,23 +1,26 @@
import { NotFoundError, SecurePassword, resolver } from "blitz"
import db from "db"
import { NotFoundError, Ctx } from "blitz"
import { prisma } from "db"
import { authenticateUser } from "./login"
import { ChangePassword } from "../validations"
import { SecurePassword } from "@blitzjs/auth"
export default resolver.pipe(
resolver.zod(ChangePassword),
resolver.authorize(),
async ({ currentPassword, newPassword }, ctx) => {
const user = await db.user.findFirst({ where: { id: ctx.session.userId! } })
if (!user) throw new NotFoundError()
export default async function changePassword(input, ctx: Ctx) {
ChangePassword.parse(input)
ctx.session.$isAuthorized()
await authenticateUser(user.email, currentPassword)
const user = await prisma.user.findFirst({
where: {
id: ctx.session.userId as number,
},
})
const hashedPassword = await SecurePassword.hash(newPassword.trim())
await db.user.update({
where: { id: user.id },
data: { hashedPassword },
})
if (!user) throw new NotFoundError()
await authenticateUser(user.email, input.currentPassword)
return true
}
)
const hashedPassword = await SecurePassword.hash(input.newPassword.trim())
await prisma.user.update({
where: { id: user.id },
data: { hashedPassword },
})
}

View File

@@ -1,10 +1,11 @@
import { hash256, Ctx } from "blitz"
import { prisma } from "db"
import { hash256 } from "@blitzjs/auth"
import forgotPassword from "./forgotPassword"
import db from "db"
import previewEmail from "preview-email"
import { Ctx } from "@blitzjs/next"
beforeEach(async () => {
await db.$reset()
await prisma.$reset()
})
const generatedToken = "plain-token"
@@ -21,7 +22,7 @@ describe("forgotPassword mutation", () => {
it("works correctly", async () => {
// Create test user
const user = await db.user.create({
const user = await prisma.user.create({
data: {
email: "user@example.com",
tokens: {
@@ -40,7 +41,7 @@ describe("forgotPassword mutation", () => {
// Invoke the mutation
await forgotPassword({ email: user.email }, {} as Ctx)
const tokens = await db.token.findMany({ where: { userId: user.id } })
const tokens = await prisma.token.findMany({ where: { userId: user.id } })
const token = tokens[0]
if (!user.tokens[0]) throw new Error("Missing user token")
if (!token) throw new Error("Missing token")

View File

@@ -1,13 +1,15 @@
import { resolver, generateToken, hash256 } from "blitz"
import db from "db"
import { prisma } from "db"
import { generateToken, hash256 } from "@blitzjs/auth"
import { forgotPasswordMailer } from "mailers/forgotPasswordMailer"
import { ForgotPassword } from "../validations"
import { Ctx } from "@blitzjs/next"
const RESET_PASSWORD_TOKEN_EXPIRATION_IN_HOURS = 4
export default resolver.pipe(resolver.zod(ForgotPassword), async ({ email }) => {
export default async function forgotPassword(input, ctx: Ctx) {
ForgotPassword.parse(input)
// 1. Get the user
const user = await db.user.findFirst({ where: { email: email.toLowerCase() } })
const user = await prisma.user.findFirst({ where: { email: input.email.toLowerCase() } })
// 2. Generate the token and expiration date.
const token = generateToken()
@@ -18,9 +20,9 @@ export default resolver.pipe(resolver.zod(ForgotPassword), async ({ email }) =>
// 3. If user with this email was found
if (user) {
// 4. Delete any existing password reset tokens
await db.token.deleteMany({ where: { type: "RESET_PASSWORD", userId: user.id } })
await prisma.token.deleteMany({ where: { type: "RESET_PASSWORD", userId: user.id } })
// 5. Save this new token in the database.
await db.token.create({
await prisma.token.create({
data: {
user: { connect: { id: user.id } },
type: "RESET_PASSWORD",
@@ -38,4 +40,4 @@ export default resolver.pipe(resolver.zod(ForgotPassword), async ({ email }) =>
// 8. Return the same result whether a password reset email was sent or not
return
})
}

View File

@@ -1,11 +1,11 @@
import { resolver, SecurePassword, AuthenticationError } from "blitz"
import db from "db"
import { AuthenticationError } from "blitz"
import { prisma } from "db"
import { Login } from "../validations"
import { Role } from "types"
import { SecurePassword } from "@blitzjs/auth"
export const authenticateUser = async (rawEmail: string, rawPassword: string) => {
const {email, password} = Login.parse({email: rawEmail, password: rawPassword})
const user = await db.user.findFirst({ where: { email } })
const { email, password } = Login.parse({ email: rawEmail, password: rawPassword })
const user = await prisma.user.findFirst({ where: { email } })
if (!user) throw new AuthenticationError()
const result = await SecurePassword.verify(user.hashedPassword, password)
@@ -13,18 +13,16 @@ export const authenticateUser = async (rawEmail: string, rawPassword: string) =>
if (result === SecurePassword.VALID_NEEDS_REHASH) {
// Upgrade hashed password with a more secure hash
const improvedHash = await SecurePassword.hash(password)
await db.user.update({ where: { id: user.id }, data: { hashedPassword: improvedHash } })
await prisma.user.update({ where: { id: user.id }, data: { hashedPassword: improvedHash } })
}
const { hashedPassword, ...rest } = user
return rest
}
export default resolver.pipe(resolver.zod(Login), async ({ email, password }, ctx) => {
// This throws an error if credentials are invalid
const user = await authenticateUser(email, password)
await ctx.session.$create({ userId: user.id, role: user.role as Role })
export default async function login(input, ctx) {
const user = await authenticateUser(input.email, input.password)
await ctx.session.$create({ userId: user.id, role: user.role })
return user
})
}

View File

@@ -1,9 +1,9 @@
import resetPassword from "./resetPassword"
import db from "db"
import { hash256, SecurePassword } from "blitz"
import { prisma } from "db"
import { SecurePassword, hash256 } from "@blitzjs/auth"
beforeEach(async () => {
await db.$reset()
await prisma.$reset()
})
const mockCtx: any = {
@@ -24,7 +24,7 @@ describe("resetPassword mutation", () => {
const past = new Date()
past.setHours(past.getHours() - 4)
const user = await db.user.create({
const user = await prisma.user.create({
data: {
email: "user@example.com",
tokens: {
@@ -70,11 +70,11 @@ describe("resetPassword mutation", () => {
)
// Delete's the token
const numberOfTokens = await db.token.count({ where: { userId: user.id } })
const numberOfTokens = await prisma.token.count({ where: { userId: user.id } })
expect(numberOfTokens).toBe(0)
// Updates user's password
const updatedUser = await db.user.findFirst({ where: { id: user.id } })
const updatedUser = await prisma.user.findFirst({ where: { id: user.id } })
expect(await SecurePassword.verify(updatedUser!.hashedPassword, newPassword)).toBe(
SecurePassword.VALID
)

View File

@@ -1,5 +1,5 @@
import { resolver, SecurePassword, hash256 } from "blitz"
import db from "db"
import { SecurePassword, hash256 } from "@blitzjs/auth"
import { prisma } from "db"
import { ResetPassword } from "../validations"
import login from "./login"
@@ -8,10 +8,11 @@ export class ResetPasswordError extends Error {
message = "Reset password link is invalid or it has expired."
}
export default resolver.pipe(resolver.zod(ResetPassword), async ({ password, token }, ctx) => {
export default async function resetPassword(input, ctx) {
ResetPassword.parse(input)
// 1. Try to find this token in the database
const hashedToken = hash256(token)
const possibleToken = await db.token.findFirst({
const hashedToken = hash256(input.token)
const possibleToken = await prisma.token.findFirst({
where: { hashedToken, type: "RESET_PASSWORD" },
include: { user: true },
})
@@ -23,7 +24,7 @@ export default resolver.pipe(resolver.zod(ResetPassword), async ({ password, tok
const savedToken = possibleToken
// 3. Delete token so it can't be used again
await db.token.delete({ where: { id: savedToken.id } })
await prisma.token.delete({ where: { id: savedToken.id } })
// 4. If token has expired, error
if (savedToken.expiresAt < new Date()) {
@@ -31,17 +32,17 @@ export default resolver.pipe(resolver.zod(ResetPassword), async ({ password, tok
}
// 5. Since token is valid, now we can update the user's password
const hashedPassword = await SecurePassword.hash(password.trim())
const user = await db.user.update({
const hashedPassword = await SecurePassword.hash(input.password.trim())
const user = await prisma.user.update({
where: { id: savedToken.userId },
data: { hashedPassword },
})
// 6. Revoke all existing login sessions for this user
await db.session.deleteMany({ where: { userId: user.id } })
await prisma.session.deleteMany({ where: { userId: user.id } })
// 7. Now log the user in with the new credentials
await login({ email: user.email, password }, ctx)
await login({ email: user.email, password: input.password }, ctx)
return true
})
}

View File

@@ -1,15 +1,19 @@
import { resolver, SecurePassword } from "blitz"
import db from "db"
import { Signup } from "app/auth/validations"
import { Role } from "types"
import { prisma } from "db"
import { SecurePassword } from "@blitzjs/auth"
export default resolver.pipe(resolver.zod(Signup), async ({ email, password }, ctx) => {
const hashedPassword = await SecurePassword.hash(password.trim())
const user = await db.user.create({
data: { email: email.toLowerCase().trim(), hashedPassword, role: "USER" },
export default async function signup(input, ctx) {
const blitzContext = ctx
const hashedPassword = await SecurePassword.hash((input.password as string) || "test-password")
const email = (input.email as string) || "test" + Math.random() + "@test.com"
const user = await prisma.user.create({
data: { email, hashedPassword, role: "user" },
select: { id: true, name: true, email: true, role: true },
})
await ctx.session.$create({ userId: user.id, role: user.role as Role })
return user
})
await blitzContext.session.$create({
userId: user.id,
})
return { userId: blitzContext.session.userId, ...user, email: input.email }
}

View File

@@ -1,59 +0,0 @@
import { BlitzPage, useRouterQuery, Link, useMutation, Routes } from "blitz"
import Layout from "app/core/layouts/Layout"
import { LabeledTextField } from "app/core/components/LabeledTextField"
import { Form, FORM_ERROR } from "app/core/components/Form"
import { ResetPassword } from "app/auth/validations"
import resetPassword from "app/auth/mutations/resetPassword"
const ResetPasswordPage: BlitzPage = () => {
const query = useRouterQuery()
const [resetPasswordMutation, { isSuccess }] = useMutation(resetPassword)
return (
<div>
<h1>Set a New Password</h1>
{isSuccess ? (
<div>
<h2>Password Reset Successfully</h2>
<p>
Go to the <Link href={Routes.Home()}>homepage</Link>
</p>
</div>
) : (
<Form
submitText="Reset Password"
schema={ResetPassword}
initialValues={{ password: "", passwordConfirmation: "", token: query.token as string }}
onSubmit={async (values) => {
try {
await resetPasswordMutation(values)
} catch (error: any) {
if (error.name === "ResetPasswordError") {
return {
[FORM_ERROR]: error.message,
}
} else {
return {
[FORM_ERROR]: "Sorry, we had an unexpected error. Please try again.",
}
}
}
}}
>
<LabeledTextField name="password" label="New Password" type="password" />
<LabeledTextField
name="passwordConfirmation"
label="Confirm New Password"
type="password"
/>
</Form>
)}
</div>
)
}
ResetPasswordPage.redirectAuthenticatedTo = "/"
ResetPasswordPage.getLayout = (page) => <Layout title="Reset Your Password">{page}</Layout>
export default ResetPasswordPage

View File

@@ -1,18 +0,0 @@
import { useRouter, BlitzPage, Routes } from "blitz"
import Layout from "app/core/layouts/Layout"
import { SignupForm } from "app/auth/components/SignupForm"
const SignupPage: BlitzPage = () => {
const router = useRouter()
return (
<div>
<SignupForm onSuccess={() => router.push(Routes.Home())} />
</div>
)
}
SignupPage.redirectAuthenticatedTo = "/"
SignupPage.getLayout = (page) => <Layout title="Sign Up">{page}</Layout>
export default SignupPage

View File

@@ -0,0 +1,20 @@
import { AuthClientPlugin } from "@blitzjs/auth"
import { setupClient } from "@blitzjs/next"
import { BlitzRpcPlugin } from "@blitzjs/rpc"
const { withBlitz } = setupClient({
plugins: [
AuthClientPlugin({
cookiePrefix: "__safeNameSlug__-cookie-prefix",
}),
BlitzRpcPlugin({
reactQueryOptions: {
queries: {
staleTime: 7000,
},
},
}),
],
})
export { withBlitz }

View File

@@ -0,0 +1,17 @@
import { setupBlitz } from "@blitzjs/next"
import { AuthServerPlugin, PrismaStorage } from "@blitzjs/auth"
import { prisma as db } from "../db/index"
import { simpleRolesIsAuthorized } from "@blitzjs/auth"
const { gSSP, gSP, api } = setupBlitz({
plugins: [
AuthServerPlugin({
cookiePrefix: "__safeNameSlug__-cookie-prefix",
// TODO fix type
storage: PrismaStorage(db as any),
isAuthorized: simpleRolesIsAuthorized,
}),
],
})
export { gSSP, gSP, api }

View File

@@ -1,4 +1,4 @@
import { useQuery } from "blitz"
import { useQuery } from "@blitzjs/rpc"
import getCurrentUser from "app/users/queries/getCurrentUser"
export const useCurrentUser = () => {

View File

@@ -1,6 +1,7 @@
import { Head, BlitzLayout } from "blitz"
import Head from "next/head"
import React, { FC } from "react"
const Layout: BlitzLayout<{title?: string}> = ({ title, children }) => {
const Layout: FC<{ title?: string; children?: React.ReactNode }> = ({ title, children }) => {
return (
<>
<Head>

View File

@@ -1,40 +0,0 @@
import {
AppProps,
ErrorBoundary,
ErrorComponent,
AuthenticationError,
AuthorizationError,
ErrorFallbackProps,
useQueryErrorResetBoundary,
} from "blitz"
import LoginForm from "app/auth/components/LoginForm"
export default function App({ Component, pageProps }: AppProps) {
const getLayout = Component.getLayout || ((page) => page)
return (
<ErrorBoundary
FallbackComponent={RootErrorFallback}
onReset={useQueryErrorResetBoundary().reset}
>
{getLayout(<Component {...pageProps} />)}
</ErrorBoundary>
)
}
function RootErrorFallback({ error, resetErrorBoundary }: ErrorFallbackProps) {
if (error instanceof AuthenticationError) {
return <LoginForm onSuccess={resetErrorBoundary} />
} else if (error instanceof AuthorizationError) {
return (
<ErrorComponent
statusCode={error.statusCode}
title="Sorry, you are not authorized to access this"
/>
)
} else {
return (
<ErrorComponent statusCode={error.statusCode || 400} title={error.message || error.name} />
)
}
}

View File

@@ -1,23 +0,0 @@
import { Document, Html, DocumentHead, Main, BlitzScript /*DocumentContext*/ } from "blitz"
class MyDocument extends Document {
// Only uncomment if you need to customize this behaviour
// static async getInitialProps(ctx: DocumentContext) {
// const initialProps = await Document.getInitialProps(ctx)
// return {...initialProps}
// }
render() {
return (
<Html lang="en">
<DocumentHead />
<body>
<Main />
<BlitzScript />
</body>
</Html>
)
}
}
export default MyDocument

View File

@@ -1,25 +0,0 @@
import { render } from "test/utils"
import Home from "./index"
import { useCurrentUser } from "app/core/hooks/useCurrentUser"
jest.mock("app/core/hooks/useCurrentUser")
const mockUseCurrentUser = useCurrentUser as jest.MockedFunction<typeof useCurrentUser>
test.skip("renders blitz documentation link", () => {
// This is an example of how to ensure a specific item is in the document
// But it's disabled by default (by test.skip) so the test doesn't fail
// when you remove the the default content from the page
// This is an example on how to mock api hooks when testing
mockUseCurrentUser.mockReturnValue({
id: 1,
name: "User",
email: "user@email.com",
role: "user",
})
const { getByText } = render(<Home />)
const linkElement = getByText(/Documentation/i)
expect(linkElement).toBeInTheDocument()
})

View File

@@ -1,272 +0,0 @@
import { Suspense } from "react"
import { Image, Link, BlitzPage, useMutation, Routes } from "blitz"
import Layout from "app/core/layouts/Layout"
import { useCurrentUser } from "app/core/hooks/useCurrentUser"
import logout from "app/auth/mutations/logout"
import logo from "public/logo.png"
/*
* This file is just for a pleasant getting started page for your new app.
* You can delete everything in here and start from scratch if you like.
*/
const UserInfo = () => {
const currentUser = useCurrentUser()
const [logoutMutation] = useMutation(logout)
if (currentUser) {
return (
<>
<button
className="button small"
onClick={async () => {
await logoutMutation()
}}
>
Logout
</button>
<div>
User id: <code>{currentUser.id}</code>
<br />
User role: <code>{currentUser.role}</code>
</div>
</>
)
} else {
return (
<>
<Link href={Routes.SignupPage()}>
<a className="button small">
<strong>Sign Up</strong>
</a>
</Link>
<Link href={Routes.LoginPage()}>
<a className="button small">
<strong>Login</strong>
</a>
</Link>
</>
)
}
}
const Home: BlitzPage = () => {
return (
<div className="container">
<main>
<div className="logo">
<Image src={logo} alt="blitzjs" />
</div>
<p>
<strong>Congrats!</strong> Your app is ready, including user sign-up and log-in.
</p>
<div className="buttons" style={{ marginTop: "1rem", marginBottom: "1rem" }}>
<Suspense fallback="Loading...">
<UserInfo />
</Suspense>
</div>
<p>
<strong>
To add a new model to your app, <br />
run the following in your terminal:
</strong>
</p>
<pre>
<code>blitz generate all project name:string</code>
</pre>
<div style={{ marginBottom: "1rem" }}>(And select Yes to run prisma migrate)</div>
<div>
<p>
Then <strong>restart the server</strong>
</p>
<pre>
<code>Ctrl + c</code>
</pre>
<pre>
<code>blitz dev</code>
</pre>
<p>
and go to{" "}
<Link href="/projects">
<a>/projects</a>
</Link>
</p>
</div>
<div className="buttons" style={{ marginTop: "5rem" }}>
<a
className="button"
href="https://blitzjs.com/docs/getting-started?utm_source=blitz-new&utm_medium=app-template&utm_campaign=blitz-new"
target="_blank"
rel="noopener noreferrer"
>
Documentation
</a>
<a
className="button-outline"
href="https://github.com/blitz-js/blitz"
target="_blank"
rel="noopener noreferrer"
>
Github Repo
</a>
<a
className="button-outline"
href="https://discord.blitzjs.com"
target="_blank"
rel="noopener noreferrer"
>
Discord Community
</a>
</div>
</main>
<footer>
<a
href="https://blitzjs.com?utm_source=blitz-new&utm_medium=app-template&utm_campaign=blitz-new"
target="_blank"
rel="noopener noreferrer"
>
Powered by Blitz.js
</a>
</footer>
<style jsx global>{`
@import url("https://fonts.googleapis.com/css2?family=Libre+Franklin:wght@300;700&display=swap");
html,
body {
padding: 0;
margin: 0;
font-family: "Libre Franklin", -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
}
* {
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
box-sizing: border-box;
}
.container {
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
main {
padding: 5rem 0;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
main p {
font-size: 1.2rem;
}
p {
text-align: center;
}
footer {
width: 100%;
height: 60px;
border-top: 1px solid #eaeaea;
display: flex;
justify-content: center;
align-items: center;
background-color: #45009d;
}
footer a {
display: flex;
justify-content: center;
align-items: center;
}
footer a {
color: #f4f4f4;
text-decoration: none;
}
.logo {
margin-bottom: 2rem;
}
.logo img {
width: 300px;
}
.buttons {
display: grid;
grid-auto-flow: column;
grid-gap: 0.5rem;
}
.button {
font-size: 1rem;
background-color: #6700eb;
padding: 1rem 2rem;
color: #f4f4f4;
text-align: center;
}
.button.small {
padding: 0.5rem 1rem;
}
.button:hover {
background-color: #45009d;
}
.button-outline {
border: 2px solid #6700eb;
padding: 1rem 2rem;
color: #6700eb;
text-align: center;
}
.button-outline:hover {
border-color: #45009d;
color: #45009d;
}
pre {
background: #fafafa;
border-radius: 5px;
padding: 0.75rem;
text-align: center;
}
code {
font-size: 0.9rem;
font-family: Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono,
Bitstream Vera Sans Mono, Courier New, monospace;
}
.grid {
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
max-width: 800px;
margin-top: 3rem;
}
@media (max-width: 600px) {
.grid {
width: 100%;
flex-direction: column;
}
}
`}</style>
</div>
)
}
Home.suppressFirstRenderFlicker = true
Home.getLayout = (page) => <Layout title="Home">{page}</Layout>
export default Home

View File

@@ -1,11 +1,11 @@
import { Ctx } from "blitz"
import db from "db"
import { prisma } from "db"
export default async function getCurrentUser(_ = null, { session }: Ctx) {
if (!session.userId) return null
const user = await db.user.findFirst({
where: { id: session.userId },
const user = await prisma.user.findFirst({
where: { id: session.userId as number },
select: { id: true, name: true, email: true, role: true },
})

View File

@@ -1,4 +0,0 @@
module.exports = {
presets: ["blitz/babel"],
plugins: [],
}

Some files were not shown because too many files have changed in this diff Show More