Skip to content

Commit

Permalink
Merge pull request #1104 from digitallyinduced/jannis/link-api-docs-i…
Browse files Browse the repository at this point in the history
…n-guide

link api docs in guide
  • Loading branch information
mpscholten authored Oct 6, 2021
2 parents 994e752 + 8ffbf28 commit d1409eb
Show file tree
Hide file tree
Showing 27 changed files with 370 additions and 374 deletions.
2 changes: 1 addition & 1 deletion Guide/assets.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ To avoid this problem, web applications typically append a hash to the url of yo
<script src="/app.js?v=19319eb"></script>
```

IHP provides an `assetPath` view helper to automatically add these hashes:
IHP provides an [`assetPath`](https://ihp.digitallyinduced.com/api-docs/IHP-Assets-ViewFunctions.html#v:assetPath) view helper to automatically add these hashes:

```haskell
[hsx|
Expand Down
22 changes: 11 additions & 11 deletions Guide/authentication.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ instance HasNewSessionUrl User where
type instance CurrentUserRecord = User
```

The `instance HasNewSessionUrl User` tells the auth module where to redirect a user in case the user tries to access a action that requires login. The definition of `CurrentUserRecord` tells the auth system to use our `User` type within the login system.
The `instance HasNewSessionUrl User` tells the auth module where to redirect a user in case the user tries to access a action that requires login. The definition of [`CurrentUserRecord`](https://ihp.digitallyinduced.com/api-docs/IHP-LoginSupport-Types.html#t:CurrentUserRecord) tells the auth system to use our `User` type within the login system.

We also need to add the type definitions for the `SessionsController`:

Expand Down Expand Up @@ -134,7 +134,7 @@ import IHP.LoginSupport.Middleware
import Web.Controller.Sessions
```

We then need to mount our session controller by adding `parseRoute @SessionController`:
We then need to mount our session controller by adding [`parseRoute @SessionController`](https://ihp.digitallyinduced.com/api-docs/IHP-RouterSupport.html#v:parseRoute):

```haskell
instance FrontController WebApplication where
Expand All @@ -154,7 +154,7 @@ instance InitControllerContext WebApplication where
initAutoRefresh
```

We need to extend this function with a `initAuthentication @User` like this:
We need to extend this function with a [`initAuthentication @User`](https://ihp.digitallyinduced.com/api-docs/IHP-LoginSupport-Middleware.html#v:initAuthentication) like this:

```haskell
instance InitControllerContext WebApplication where
Expand All @@ -164,7 +164,7 @@ instance InitControllerContext WebApplication where
initAuthentication @User
```

This will fetch the user from the database when a `userId` is given in the session. The fetched user record is saved to the special `?context` variable and is used by all the helper functions like `currentUser`.
This will fetch the user from the database when a `userId` is given in the session. The fetched user record is saved to the special `?context` variable and is used by all the helper functions like [`currentUser`](https://ihp.digitallyinduced.com/api-docs/IHP-LoginSupport-Helper-Controller.html#v:currentUser).


## Trying out the login
Expand All @@ -177,17 +177,17 @@ After you have completed the above steps, you can open the login at `/NewSession

## Accessing the current user

Inside your actions you can then use `currentUser` to get access to the current logged in user:
Inside your actions you can then use [`currentUser`](https://ihp.digitallyinduced.com/api-docs/IHP-LoginSupport-Helper-Controller.html#v:currentUser) to get access to the current logged in user:

```haskell
action MyAction = do
let text = "Hello " <> (get #email currentUser)
renderPlain text
```

In case the user is logged out, an exception will be thrown when accessing `currentUser` and the browser will automatically be redirected to the `NewSessionAction`.
In case the user is logged out, an exception will be thrown when accessing [`currentUser`](https://ihp.digitallyinduced.com/api-docs/IHP-LoginSupport-Helper-Controller.html#v:currentUser) and the browser will automatically be redirected to the `NewSessionAction`.

You can use `currentUserOrNothing` to manually deal with the not-logged-in case:
You can use [`currentUserOrNothing`](https://ihp.digitallyinduced.com/api-docs/IHP-LoginSupport-Helper-Controller.html#v:currentUserOrNothing) to manually deal with the not-logged-in case:

```haskell
action MyAction = do
Expand All @@ -198,9 +198,9 @@ action MyAction = do
Nothing -> renderPlain "Please login first"
```

Additionally you can use `currentUserId` as a shortcut for `currentUser |> get #id`.
Additionally you can use [`currentUserId`](https://ihp.digitallyinduced.com/api-docs/IHP-LoginSupport-Helper-Controller.html#v:currentUserId) as a shortcut for `currentUser |> get #id`.

You can also access the user using `currentUser` inside your views:
You can also access the user using [`currentUser`](https://ihp.digitallyinduced.com/api-docs/IHP-LoginSupport-Helper-Controller.html#v:currentUser) inside your views:

```html
[hsx|
Expand All @@ -210,7 +210,7 @@ You can also access the user using `currentUser` inside your views:

## Performing actions on login

The sessioncontroller has a convenient `beforeLogin` which is run on login after the user is authenticated, but before the target page is rendered. This can be useful for updating last login time, number of logins or aborting the login when the user is blocked. Add code for it in your `Web/Controller/Sessions.hs`. To update number of logins (requires `logins` integer field in `Users` table):
The sessioncontroller has a convenient [`beforeLogin`](https://ihp.digitallyinduced.com/api-docs/IHP-AuthSupport-Controller-Sessions.html#v:beforeLogin) which is run on login after the user is authenticated, but before the target page is rendered. This can be useful for updating last login time, number of logins or aborting the login when the user is blocked. Add code for it in your `Web/Controller/Sessions.hs`. To update number of logins (requires `logins` integer field in `Users` table):

```haskell
instance Sessions.SessionsControllerConfig User where
Expand All @@ -223,7 +223,7 @@ updateLoginHistory user = do
pure ()
```

To block login (requires `isConfirmed`boolean field in `Users` table):
To block login (requires `isConfirmed` boolean field in `Users` table):

```haskell
instance Sessions.SessionsControllerConfig User where
Expand Down
8 changes: 4 additions & 4 deletions Guide/authorization.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

## Restricting an action to logged-in users

To restrict an action to a logged-in user, use `ensureIsUser`:
To restrict an action to a logged-in user, use [`ensureIsUser`](https://ihp.digitallyinduced.com/api-docs/IHP-LoginSupport-Helper-Controller.html#v:ensureIsUser):

```haskell
action PostsAction = do
Expand All @@ -17,7 +17,7 @@ action PostsAction = do

When someone is trying to access the `PostsAction` but is not logged-in, the browser will be redirected to the login page. After the login succeeded, the user will be redirected back to the `PostsAction`.

It's common to restrict all actions inside a controller to logged-in users only. Place the `ensureIsUser` inside the `beforeAction` hook to automatically apply it to all actions:
It's common to restrict all actions inside a controller to logged-in users only. Place the [`ensureIsUser`](https://ihp.digitallyinduced.com/api-docs/IHP-LoginSupport-Helper-Controller.html#v:ensureIsUser) inside the [`beforeAction`](https://ihp.digitallyinduced.com/api-docs/IHP-ControllerSupport.html#v:beforeAction) hook to automatically apply it to all actions:

```haskell
instance Controller PostsController where
Expand All @@ -36,7 +36,7 @@ In this case `PostsAction` and `ShowPostAction` are only accessible to logged-in

## Restricting an action to logged-in admins

To restrict an action to a logged-in admin, use `ensureIsAdmin` instead of `ensureIsUser`. If you get
To restrict an action to a logged-in admin, use [`ensureIsAdmin`](https://ihp.digitallyinduced.com/api-docs/IHP-ControllerSupport.html#v:beforeAction) instead of [`ensureIsUser`](https://ihp.digitallyinduced.com/api-docs/IHP-LoginSupport-Helper-Controller.html#v:ensureIsUser). If you get

```
error:
Expand All @@ -57,7 +57,7 @@ instance Controller UserController where

## Checking for Permissions

You can use `accessDeniedUnless` to allow certain things only for specific users. For example, to restrict a `ShowPostAction` only to the user who a post belongs to, use this:
You can use [`accessDeniedUnless`](https://ihp.digitallyinduced.com/api-docs/IHP-AuthSupport-Authorization.html#v:accessDeniedUnless) to allow certain things only for specific users. For example, to restrict a `ShowPostAction` only to the user who a post belongs to, use this:

```haskell
action ShowPostAction { postId } = do
Expand Down
8 changes: 4 additions & 4 deletions Guide/auto-refresh.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Auto Refresh offers a way to re-render views of your application when the underl

It's good to have a general understanding of how IHP Auto Refresh works.

Auto Refresh first has to be activated for an action by calling `autoRefresh`. Once activated the framework will automatically track all tables your action is using e.g. in `SELECT * FROM ...` queries. Once the action sends a response IHP will start watching for any kind of `INSERT`, `UPDATE` or `DELETE` statement to all the tables used by your action.
Auto Refresh first has to be activated for an action by calling [`autoRefresh`](https://ihp.digitallyinduced.com/api-docs/IHP-AutoRefresh.html#v:autoRefresh). Once activated the framework will automatically track all tables your action is using e.g. in `SELECT * FROM ...` queries. Once the action sends a response IHP will start watching for any kind of `INSERT`, `UPDATE` or `DELETE` statement to all the tables used by your action.

When the page is rendered a small JavaScript function will connect back to the IHP server using a WebSocket connection.

Expand All @@ -37,7 +37,7 @@ action ShowProjectAction { projectId } = do
render ShowView { .. }
```

To enable auto refresh we have to add `autoRefresh` in front of the `do`:
To enable auto refresh we have to add [`autoRefresh`](https://ihp.digitallyinduced.com/api-docs/IHP-AutoRefresh.html#v:autoRefresh) in front of the `do`:

```haskell
action ShowProjectAction { projectId } = autoRefresh do
Expand Down Expand Up @@ -106,7 +106,7 @@ action StatsAction = autoRefresh do
pure StatsView { ..}
```

When using this custom query with `sqlQuery`, Auto Refresh is not aware that we're reading from the `companies` table. In this case we need to help out Auto Refresh by calling `trackTableRead`:
When using this custom query with [`sqlQuery`](https://ihp.digitallyinduced.com/api-docs/IHP-ModelSupport.html#v:sqlQuery), Auto Refresh is not aware that we're reading from the `companies` table. In this case we need to help out Auto Refresh by calling [`trackTableRead`](https://ihp.digitallyinduced.com/api-docs/IHP-ModelSupport.html#v:trackTableRead):


```haskell
Expand All @@ -118,4 +118,4 @@ action StatsAction = autoRefresh do
pure StatsView { ..}
```

The `trackTableRead` marks the table as accessed for Auto Refresh and leads to the table being watched.
The [`trackTableRead`](https://ihp.digitallyinduced.com/api-docs/IHP-ModelSupport.html#v:trackTableRead) marks the table as accessed for Auto Refresh and leads to the table being watched.
Loading

0 comments on commit d1409eb

Please sign in to comment.