---
title: Authentication and authorization
weight: 7
---
`@auth`, `@guest`, `@can`, `@cannot` and `@canany`, with `@elseauth`, `@elseguest`, `@elsecan`, `@elsecannot`, `@elsecanany` and the matching `@end...` closers, take these shapes:
```html
@auth('admin') Welcome back @elseauth Hello @else <a href="/login">Log in</a> @endauth
@can('update-post', post.id) <a href="/edit">Edit</a> @endcan
@canany(['edit', 'delete'], post.id) Manage @endcanany
```
Sword runs no policies. In laravel-rust 0.1.0 to 0.2.0 a view engine receives no user, guard or gate, and policy checks are asynchronous typed calls, so they cannot run inside a template. Make the decisions in the handler with the application's own authorization and record the answers in an `AuthContext`, passed as the view data `auth`:
```rust,ignore
let auth = AuthContext::authenticated()
.guard("admin", false)
.ability("create-post", true)
.ability_for("update-post", post.id, post.user_id == user.id);
view("posts.show", data! { post => post, auth => auth })
```
- `@auth` and `@guest` read the default guard, or the quoted guard name (`@auth('admin')`, recorded with `guard`).
- `@can('ability')` reads a result recorded with `ability`; `@can('ability', subject)` reads one recorded with `ability_for`. The subject is a string or an integer, usually a record id; any other value is an error. Only one ability and one subject are accepted, and `@canany` takes a list of abilities and the same optional subject.
- Anything the template asks about that was not recorded, a missing `auth`, or a value that is not an `AuthContext` is a render error that names what is missing. It is never treated as allowed or denied, so a forgotten check cannot open or close a section silently. For `@canany`, every listed ability must be recorded.
- `auth` is an ordinary view data name; do not use it for anything else in views that use these directives. `@else` and `@elseif` can follow any of these openers, and each opener must end with its own closer.
- Authorization stays enforced in the handler. These directives only choose markup; a hidden link is not access control.