Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rest-express-auth example #2869

Draft
wants to merge 22 commits into
base: latest
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
dc060df
chore: initial rest-express-auth creation
gautvm May 18, 2021
6b9a670
refactor: create data models
gautvm May 18, 2021
fb38fef
refactor: set up api structure
gautvm May 18, 2021
f14f40c
refactor: start some work on signup
gautvm May 18, 2021
e151438
feat(rest-express-auth) add signup route
gautvm May 18, 2021
67a3158
feat(rest-express-auth) add login route
gautvm May 18, 2021
df0b5d8
feat(rest-express-auth) added logout route
gautvm May 18, 2021
35faffb
refactor(rest-express-auth) refactor isAuth middleware
gautvm May 18, 2021
d0324e5
feat(rest-express-auth) add account delete route
gautvm May 19, 2021
c10c571
feat(rest-express-auth): posts get route
gautvm Jun 4, 2021
6bc4221
added readmes to rest-express-auth
ruheni Jun 21, 2021
cf802ea
Merge branch 'prisma:latest' into rest-express-auth
Mar 7, 2023
e5bd3a2
fix: implemented fixes from review
gautvm Mar 7, 2023
ea1dbd3
refactor updated rest-express-auth example to follow the format of ot…
gautvm Mar 7, 2023
18e37d1
refactor: added database seeding
gautvm Mar 7, 2023
e48bfc5
fix: corrected readme
Mar 7, 2023
9e56be0
fix: added isAuthenticated middleware to routes
Mar 7, 2023
ad62f59
fix: rest-express-auth: fixed readmes
gautvm Mar 20, 2023
78494c3
fix: rest-express-auth: correct protected routes
gautvm Mar 20, 2023
9d82677
refactor: rest-express-auth: add test
gautvm Mar 20, 2023
6df2873
fix: rest-express-auth: add hashing for passwords in seeding data
gautvm Mar 20, 2023
90e30d7
docs: rest-express-auth: add docs for isAuthenticated middleware
gautvm May 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/readmes/_using-the-rest-api-auth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
## Using the REST API

You can access the REST API of the server using the following endpoints:

### `GET`

- `/post/:id`: Fetch a single post by its `id`
- `/feed?searchString={searchString}&take={take}&skip={skip}&orderBy={orderBy}`: Fetch all _published_ posts
- Query Parameters
- `searchString` (optional): This filters posts by `title` or `content`
- `take` (optional): This specifies how many objects should be returned in the list
- `skip` (optional): This specifies how many of the returned objects in the list should be skipped
- `orderBy` (optional): The sort order for posts in either ascending or descending order. The value can either `asc` or `desc`s
- `/user/:id/drafts`: (protected) Fetch user's drafts by their `id`
- `/users`: Fetch all users

### `POST`

- `/post`: Create a new post
- Body:
- `title: String` (required): The title of the post
- `content: String` (optional): The content of the post
- `authorEmail: String` (required): The email of the user that creates the post
- `/signup`: Create a new user and session
- Body:
- `email: String` (required): The email address of the user
- `password: String` (required): The email of the user
- `posts: PostCreateInput[]` (optional): The posts of the user
- `/login`: Login into account and start a new session
- Body:
- `email: String` (required): The email address of the user
- `password: String` (required): The email of the user
- `/logout`: Logs out the user and deletes the session

### `PUT`

- `/publish/:id`: Toggle the publish value of a post by its `id`
- `/post/:id/views`: Increases the `viewCount` of a `Post` by one `id`

### `DELETE`

- `/post/:id`: Delete a post by its `id`
27 changes: 27 additions & 0 deletions .github/readmes/typescript/rest-express-auth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# REST Authentication API Example

This example shows how to implement a **REST Authentication API with TypeScript** using [Express](https://expressjs.com/) and [Prisma Client](https://www.prisma.io/docs/concepts/components/prisma-client). The example uses an SQLite database file with some initial dummy data which you can find at [`./prisma/dev.db`](./prisma/dev.db).

__INLINE(../_setup-0.md)__
npx try-prisma@latest --template typescript/rest-express-auth
__INLINE(../_setup-1.md)__
cd rest-express-auth
__INLINE(../_setup-2.md)__
cd prisma-examples/typescript/rest-express-auth
__INLINE(../_setup-3.md)__

### 3. Start the REST API server

```
npm run dev
```

The server is now running on `http://localhost:3000`. You can now run the API requests, e.g. [`http://localhost:3000/feed`](http://localhost:3000/feed).

__INLINE(../../_using-the-rest-api.md)__

__INLINE(../_evolving-the-app-rest.md)__

__INLINE(../../_switching-databases.md)__

__INLINE(../../_next-steps.md)__
14 changes: 14 additions & 0 deletions .github/tests/typescript/rest-express-auth/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

set -eu

npm install
npx prisma migrate dev --name init
npm run dev &
pid=$!

sleep 20

npx newman run ../../.github/tests/postman_collections/rest.json --bail

kill "$pid"