{"id":20279714,"url":"https://github.com/genieframework/genieauthentication.jl","last_synced_at":"2025-08-09T20:11:24.368Z","repository":{"id":39645028,"uuid":"190593963","full_name":"GenieFramework/GenieAuthentication.jl","owner":"GenieFramework","description":"Authentication plugin for Genie framework","archived":false,"fork":false,"pushed_at":"2023-11-20T17:36:35.000Z","size":99,"stargazers_count":21,"open_issues_count":10,"forks_count":11,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-02-20T21:41:19.672Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Julia","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GenieFramework.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-06T14:17:53.000Z","updated_at":"2024-12-02T13:18:50.000Z","dependencies_parsed_at":"2022-07-13T10:30:44.885Z","dependency_job_id":null,"html_url":"https://github.com/GenieFramework/GenieAuthentication.jl","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GenieFramework%2FGenieAuthentication.jl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GenieFramework%2FGenieAuthentication.jl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GenieFramework%2FGenieAuthentication.jl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GenieFramework%2FGenieAuthentication.jl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GenieFramework","download_url":"https://codeload.github.com/GenieFramework/GenieAuthentication.jl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241773256,"owners_count":20018064,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-14T13:32:38.238Z","updated_at":"2025-03-04T02:41:14.814Z","avatar_url":"https://github.com/GenieFramework.png","language":"Julia","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GenieAuthentication\n\nAuthentication plugin for `Genie.jl`\n\n## Installation\n\nThe `GenieAuthentication.jl` package is an authentication plugin for `Genie.jl`, the highly productive Julia web framework.\nAs such, it requires installation within the environment of a `Genie.jl` MVC application, allowing the plugin to install its files (which include models, controllers, database migrations, plugins, and other files).\n\n### Load your `Genie.jl` app\n\nFirst load the `Genie.jl` application, for example using\n\n```bash\n$\u003e cd /path/to/your/genie_app\n\n$\u003e ./bin/repl\n```\n\nAlternatively, you can create a new `Genie.jl` MVC application (`SearchLight.jl` ORM support is required in order to store the user accounts into the database). If you are not sure how to do that, please follow the documentation for `Genie.jl`, for example at \u003chttps://genieframework.com/docs/Genie/v5/tutorials/4-1--Developing_MVC_Web_Apps.html\u003e.\n\n### Add the plugin\n\nNext, add the plugin:\n\n```julia\njulia\u003e ]\n\n(MyGenieApp) pkg\u003e add GenieAuthentication\n```\n\nOnce added, we can use its `install` function to add its files to the `Genie.jl` app (required only upon installation):\n\n```julia\njulia\u003e using GenieAuthentication\n\njulia\u003e GenieAuthentication.install(@__DIR__)\n```\n\nThe above command will set up the plugin's files within your `Genie.jl` app (will create various files including new views, controllers, models, migrations, initializers, etc).\n\n## Usage\n\nThe main plugin file should now be found in the `plugins/` folder within your `Genie.jl` app. It sets up configuration and registers routes.\n\n---\n**HEADS UP**\n\nMake sure to uncomment out the `/register` routes in `plugins/genie_authentication.jl` if you want to provide user registration features.\n\nThey are disabled by default in order to eliminate the risk of accidentally allowing random users to create accounts and expose your application.\n\n---\n\n### Set up the database\n\nThe plugin needs DB support to store user data. You will find a `*_create_table_users.jl` migration file within the `db/migrations/` folder. We need to run it:\n\n```julia\njulia\u003e using SearchLight\n\njulia\u003e SearchLight.Migration.up(\"CreateTableUsers\")\n```\n\nThis will create the necessary table.\n\n---\n**HEADS UP**\n\nIf your app wasn't already set up to work with `SearchLight.jl`, you need to add `SearchLight.jl` support first.\nPlease check the `Genie.jl` documentation on how to do that, for example at \u003chttps://genieframework.com/docs/Genie/v5/tutorials/4-1--Developing_MVC_Web_Apps.html#Connecting-to-the-database\u003e. This includes setting up a `db/connection.yml` and an empty migration table with `create_migrations_table` if it has not already been done.\n\n---\n\n### Set up the successful login route\n\nUpon a successful login, the plugin will redirect the user to the `:success` route, which invokes `AuthenticationController.success`.\n\n---\n\n### Enforcing authentication\n\nNow that we have a functional authentication system, there are two ways of enforcing authentication.\n\n#### `authenticated!()`\n\nThe `authenticated!()` function will enforce authentication - meaning that it will check if a user is authenticated, and if not, it will automatically throw an `ExceptionalResponse` and force a redirect to the `:show_login` route which displays the login form.\n\nWe can use this anywhere in our route handling code, for example within routes:\n\n```julia\n# routes.jl\nusing GenieAuthentication\n\nroute(\"/protected\") do; authenticated!()\n  # this code is only accessible for authenticated users\nend\n```\n\nOr within handler functions inside controllers:\n\n```julia\n# routes.jl\nroute(\"/protected\", ProtectedController.secret)\n```\n\n```julia\n# ProtectedController.jl\nusing GenieAuthentication\n\nfunction secret()\n  authenticated!()\n\n  # this code is only accessible for authenticated users\nend\n```\n\n---\n**HEADS UP**\n\nIf you're throwing an `ExceptionalResponse` as the result of the failed authentication, make sure to also be `using Genie.Exceptions`.\n\n---\n\n#### `authenticated()`\n\nIn addition to the imperative style of the `authenticated!()` function, we can also use the `authenticated()` function (no `!` at the end) which returns a `bool` indicated if a user is currently authenticated.\n\nIt is especially used for adding dynamic UI elements based on the state of the authentication:\n\n```html\n\u003cdiv class=\"row align-items-center\"\u003e\n  \u003cdiv class=\"col col-12 text-center\"\u003e\n    \u003c% if ! authenticated() %\u003e\n    \u003ca href=\"/login\" class=\"btn btn-light btn-lg\" style=\"color: #fff;\"\u003eLogin\u003c/a\u003e\n    \u003c% end %\u003e\n  \u003c/div\u003e\n\u003c/div\u003e\n```\n\nWe can also use it to mimic the behaviour of `authenticated!()`:\n\n```julia\nusing Genie.Exceptions\nusing GenieAuthentication\n\n# This function _can not_ be accessed without authentication\nfunction index()\n  authenticated() || throw(ExceptionalResponse(redirect(:show_login)))\n\n  h1(\"Welcome Admin\") |\u003e html\nend\n```\n\nOr to perform custom actions:\n\n```julia\nusing GenieAuthentication\n\nroute(\"/you/shant/pass\") do\n  authenticated() || return \"Can't touch this!\"\n\n  \"You're welcome!\"\nend\n```\n\n---\n\n### Adding a user\n\nYou can create a user at the REPL like this (using stronger usernames and passwords though 🙈):\n\n```julia\njulia\u003e using Users\n\njulia\u003e u = User(email = \"admin@admin\", name = \"Admin\", password = Users.hash_password(\"admin\"), username = \"admin\")\n\njulia\u003e save!(u)\n```\n\n---\n\n### Get current user information\n\nIf the user was authenticated, check first with `authenticated()`, you can obtain the current user information with `current_user()`.\n\n```julia\nusing GenieAuthentication\n\nroute(\"/your/email\") do\n  authenticated() || return \"Can't get it!\"\n  user = current_user()\n  user.email\nend\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgenieframework%2Fgenieauthentication.jl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgenieframework%2Fgenieauthentication.jl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgenieframework%2Fgenieauthentication.jl/lists"}