Skip to content

Commit

Permalink
add section about user authentication (#1690)
Browse files Browse the repository at this point in the history
* add section about user authentication

* update

* add signing in
  • Loading branch information
Slashek authored Jan 23, 2024
1 parent c53493e commit a0189c5
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Business logic and presentation logic are separated and should not interfere wit

Command is our concept to encapsulate business rules. By following our recommendation, you can improve the consistency of your code to make it easier to onboard new developers to the project and take over existing projects. We use the same pattern for all of our templates. The advantage of using this architecture is that it is easy to re-use the command - you can execute it in a live web request as well as a background job. It is also easy to copy it across different projects.

Command are located in `/app/views/partials/lib/commands`
Command are located in `app/views/partials/lib/commands`

- For business logic use commands
- A generic command consists of 3 stages:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ Currently, you are using example content and don’t have any logic here, that

## Adding visual styling with CSS

You need a CSS file here. Since the [publicly visible static files](/get-started/working-with-the-code-and-files/#basic-overview-of-the-directory-structure) are placed in the `/app/assets/` directory, you will save your CSS file as `/app/assets/styles/app.css`.
You need a CSS file here. Since the [publicly visible static files](/get-started/working-with-the-code-and-files/#basic-overview-of-the-directory-structure) are placed in the `app/assets/` directory, you will save your CSS file as `/app/assets/styles/app.css`.

We don’t need to get into details here, you can just copy this example CSS to make your app look a little nicer:

#### /app/assets/styles/app.css
#### app/assets/styles/app.css

```css
body {
Expand Down Expand Up @@ -159,7 +159,7 @@ li {
}
```

You still need to **link the CSS file on your page**. The proper place for this would be the `/app/views/layouts/application.html.liquid`:
You still need to **link the CSS file on your page**. The proper place for this would be the `app/views/layouts/application.html.liquid`:

#### app/view/layout/application.liquid

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ A simple GraphQL mutation is usually enough to remove a single entry from the da

Start by preparing a new GraphQL file that will handle the deletion of an item. Remember that when you have to delete a record from the database, you need to be able to point to it directly first. For that, you are going to use the record’s unique _ID_.

To follow the pattern established in the previous parts of this tutorial series, create the new file for the new GraphQL mutation – `/app/graphql/item/delete.graphql`:
To follow the pattern established in the previous parts of this tutorial series, create the new file for the new GraphQL mutation – `app/graphql/item/delete.graphql`:

#### /app/graphql/item/delete.graphql
#### app/graphql/item/delete.graphql

```graphql
mutation item_delete(
Expand Down Expand Up @@ -74,9 +74,9 @@ In this tutorial, you’ve prepared a working list of `To Do` items. Each of tho

The simplest way of making this work would be to use the mutation that you’ve just created. If you remove the record from the database, it will be gone from your list.

To trigger the mutation, you would require a new page that will become a bridge between the user interface and the GraphQL mutation. Create that new page under `/app/views/pages/item/delete.liquid`:
To trigger the mutation, you would require a new page that will become a bridge between the user interface and the GraphQL mutation. Create that new page under `app/views/pages/item/delete.liquid`:

#### /app/views/pages/item/delete.liquid
#### app/views/pages/item/delete.liquid

{% raw %}
```liquid
Expand Down Expand Up @@ -128,7 +128,7 @@ else

One last step you musn’t forget is to update the `index` page. You have a `form` with the button that – when clicked – should remove the item from the list. You need to update the `<form>` with the proper `action=""` and you need to pass the `id` of the item you plan to remove. Do this in a hidden `input`:

#### /app/views/pages/index.liquid
#### app/views/pages/index.liquid

{% raw %}
```liquid
Expand All @@ -141,4 +141,4 @@ One last step you musn’t forget is to update the `index` page. You have a `for

To test the data removal functionality, add an item or a few to your _To Do_ list and then click on the _Mark as done_ button, which should effectively remove that particular item from your list.

{% render 'alert/next', content: 'Sending email notifications', url: '/get-started/build-your-first-app/sending-email-notifications' %}
{% render 'alert/next', content: 'Sending email notifications', url: '/get-started/build-your-first-app/sending-email-notifications' %}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ You can display the data on your page by building a query that gets the data fro

## Getting the data from the database

Start with building a query that will pull the data from the `item` table. Any filename will be good of course, but we found that for the sake of extending the query in the future you might consider saving the query in `/app/graphql/item/search.graphql`:
Start with building a query that will pull the data from the `item` table. Any filename will be good of course, but we found that for the sake of extending the query in the future you might consider saving the query in `app/graphql/item/search.graphql`:

#### /app/graphql/item/search.graphql
#### app/graphql/item/search.graphql

```graphql
query item_search {
Expand Down Expand Up @@ -73,7 +73,7 @@ Assuming you have a page that you want to present the data on, and the query tha
```
{% endraw %}

You don’t have to use the full path to the GraphQL query as the platform knows where to look for those. That simple line will run the query (placed in `/app/graphql/item/search.graphql`) and assign the results to the variable `items`.
You don’t have to use the full path to the GraphQL query as the platform knows where to look for those. That simple line will run the query (placed in `app/graphql/item/search.graphql`) and assign the results to the variable `items`.

The results are in the form of a _hash_. If you are comming from the JavaScript or PHP world you might name those _objects_.

Expand All @@ -91,7 +91,7 @@ The results are in the form of a _hash_. If you are comming from the JavaScript

When you have the array of items, use the for loop to iterate on it and render them on the page using Liquid:

#### /app/views/pages/index.liquid
#### app/views/pages/index.liquid

{% raw %}
```liquid
Expand All @@ -112,7 +112,7 @@ This code will repeat whatever is between the {% raw %}`{% for %}`{% endraw %} a

There is one more thing you could do: display some information when there are no items to show on the page. As you have requested the number of `total_entries` in your GraphQL query, an `if` statement using it would be enough:

#### /app/views/pages/index.liquid
#### app/views/pages/index.liquid

{% raw %}
```liquid
Expand All @@ -122,9 +122,9 @@ There is one more thing you could do: display some information when there are no
```
{% endraw %}

So, the final code of `/app/views/pages/index.liquid` would look like this:
So, the final code of `app/views/pages/index.liquid` would look like this:

#### /app/views/pages/index.liquid
#### app/views/pages/index.liquid

{% raw %}
```liquid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ You would also need a **unique ID** for every record you store, but platformOS c
| 2 | Clean up in the kitchen |
| 3 | Build my first app on platformOS |

To build a database table on platformOS, you will have to *create a file* that will tell the platform what _properties_ (or _columns_ if you are used to think about the database as a table) you need and what type of data you would like to store in them. Those files need to be placed in a dedicated directory: `/app/schema/`.
To build a database table on platformOS, you will have to *create a file* that will tell the platform what _properties_ (or _columns_ if you are used to think about the database as a table) you need and what type of data you would like to store in them. Those files need to be placed in a dedicated directory: `app/schema/`.

To describe the table structure, use the data-serialization language called YAML. For a basic table like yours, create a file named `item.yml`:

#### /app/schema/item.yml
#### app/schema/item.yml

```yaml
name: item
Expand Down Expand Up @@ -75,13 +75,13 @@ If everything went right, you should see the newly created `item` table on the l

## Save the data in the database

To actually save some data in the database, you need to start by creating a GraphQL mutation in the `/app/graphql/` directory.
To actually save some data in the database, you need to start by creating a GraphQL mutation in the `app/graphql/` directory.

In this example, you only have a single table in your application, so you could put the file directly in that folder, but when your application will grow, you might want to consider a more standarized way of organizing the queries.

So, to accommodate for future growth, save the file in `/app/graphql/item/create.graphql`:
So, to accommodate for future growth, save the file in `app/graphql/item/create.graphql`:

#### /app/graphql/item/create.graphql
#### app/graphql/item/create.graphql

```graphql
mutation item_create(
Expand Down Expand Up @@ -158,9 +158,9 @@ To save the data that the user provided in the application you need three things

If you are following this tutorial, you already have a form on your index page, you just wrote the GraphQL query, so the only thing left would be to build a page that will take the data from the `<form>` and pass it to the query.

To do this, create a new page: `/app/views/pages/item/create.liquid`:
To do this, create a new page: `app/views/pages/item/create.liquid`:

#### /app/views/pages/item/create.liquid
#### app/views/pages/item/create.liquid

{% raw %}
```liquid
Expand Down Expand Up @@ -199,7 +199,7 @@ When you want to write more Liquid than a single line, put the code between {% r
```
{% endraw %}

This is a place where you execute your GraphQL query. You run the query located in `/app/graphql/item/create.graphql`, but you don’t need to pass the whole path, as the platform knows where to look for the queries. You defined a variable called $title in your graphql, so you need to pass it to the query, but you don’t want it to be empty, that’s why you assign a value context.params.title to it. All query params passed to the page will be always available in the context.params variable.
This is a place where you execute your GraphQL query. You run the query located in `app/graphql/item/create.graphql`, but you don’t need to pass the whole path, as the platform knows where to look for the queries. You defined a variable called $title in your graphql, so you need to pass it to the query, but you don’t want it to be empty, that’s why you assign a value context.params.title to it. All query params passed to the page will be always available in the context.params variable.

{% raw %}
```liquid
Expand Down Expand Up @@ -230,9 +230,9 @@ endif

If you have never seen Liquid before, don’t worry, the syntax will become familiar after a little bit of using it.

The last thing left for you to do before testing would be to add the URL to your newly created page in the `<form>` so that when the user submits the form, it will redirect to the page that controlls the process of saving the data. Let’s get back to `/app/view/pages/index.liquid` and update the code:
The last thing left for you to do before testing would be to add the URL to your newly created page in the `<form>` so that when the user submits the form, it will redirect to the page that controlls the process of saving the data. Let’s get back to `app/view/pages/index.liquid` and update the code:

#### /app/view/pages/index.liquid
#### app/view/pages/index.liquid

```html
<form action="/item/create" method="post">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ platformOS treats emails similarly to standard HTML pages. You can create a sepa

Assuming, for the purpose of this tutorial, that you would like an independent HTML structure for all of your emails, you can create a new layout in the `app/views/layout/` folder, in a file called `mailer.liquid`. The name can be anything you want.

#### /app/views/layout/mailer.liquid
#### app/views/layout/mailer.liquid

{% raw %}
```liquid
Expand Down Expand Up @@ -73,7 +73,7 @@ If you ever styled an email, you know how many limitations the CSS for emails ha

For your _To Do_ app, add an email notification when you add a new item on the list. The content of email messages should be kept in the `app/emails/` folder. To follow the pattern you established in this tutorial you’re going to create the file as `app/emails/item/created.liquid`:

#### /app/emails/item/created.liquid
#### app/emails/item/created.liquid

{% raw %}
```liquid
Expand Down Expand Up @@ -102,7 +102,7 @@ The last line is just the content of the email you’re going to send. Since you
To actually send an email through the platform you need to trigger it via GraphQL. To make it as simple as you can, create a new file under `app/graphql/item/email/created.graphql` that will handle sending the email after you create a new item in the database:


#### /app/graphql/item/email/created.graphql
#### app/graphql/item/email/created.graphql

```graphql
mutation email_item_created(
Expand Down Expand Up @@ -145,7 +145,7 @@ If you are following this tutorial and building the _To Do_ app, then you probab

Since you have a separate page that handles this (`app\views\pages\item\create.liquid`), you can just plug into the right place and trigger the GraphQL mutation after the new item was successfully saved in the database:

#### /app/views/pages/item/create.liquid
#### app/views/pages/item/create.liquid

{% raw %}
```liquid
Expand Down Expand Up @@ -185,7 +185,7 @@ In the `app/emails/item/created.liquid` you’ve hardcoded the `to:` address, bu

It is totally possible to use a variable in the configuration section of a page. As an example, in `app/emails/item/created.liquid` you could use `{% raw %}{{ data.recipient }}{% endraw %}`:

#### /app/emails/item/created.liquid
#### app/emails/item/created.liquid

{% raw %}
```yaml
Expand All @@ -202,7 +202,7 @@ Notice that the Liquid variable is surrounded with quotation marks.

This would also mean that you need to pass the `recipient` variable to your GraphQL mutation. Since you are passing the whole `context.params` as described above, you could use a hidden form element in the `<form>`:

#### /app/views/pages/index.liquid
#### app/views/pages/index.liquid

```html
<!-- form for adding new item -->
Expand Down
Loading

0 comments on commit a0189c5

Please sign in to comment.