├── .formatter.exs ├── .gitignore ├── README.md ├── assets ├── css │ ├── app.css │ └── phoenix.css ├── js │ └── app.js └── vendor │ └── topbar.js ├── config ├── config.exs ├── dev.exs ├── prod.exs ├── runtime.exs └── test.exs ├── lib ├── sqlite_scale.ex ├── sqlite_scale │ ├── accounts.ex │ ├── accounts │ │ ├── user.ex │ │ ├── user_notifier.ex │ │ └── user_token.ex │ ├── application.ex │ ├── dynamic_repo_supervisor.ex │ ├── dynamic_repo_supervisor │ │ ├── repo_hydrator.ex │ │ ├── repo_registry.ex │ │ └── repo_supervisor.ex │ ├── mailer.ex │ ├── repo.ex │ ├── todo_items.ex │ ├── todo_items │ │ └── todo_item.ex │ └── user_repo.ex ├── sqlite_scale_web.ex └── sqlite_scale_web │ ├── controllers │ ├── page_controller.ex │ ├── todo_item_controller.ex │ ├── user_auth.ex │ ├── user_confirmation_controller.ex │ ├── user_registration_controller.ex │ ├── user_reset_password_controller.ex │ ├── user_session_controller.ex │ └── user_settings_controller.ex │ ├── endpoint.ex │ ├── gettext.ex │ ├── router.ex │ ├── telemetry.ex │ ├── templates │ ├── layout │ │ ├── _user_menu.html.heex │ │ ├── app.html.heex │ │ ├── live.html.heex │ │ └── root.html.heex │ ├── page │ │ └── index.html.heex │ ├── todo_item │ │ ├── edit.html.heex │ │ ├── form.html.heex │ │ ├── index.html.heex │ │ ├── new.html.heex │ │ └── show.html.heex │ ├── user_confirmation │ │ ├── edit.html.heex │ │ └── new.html.heex │ ├── user_registration │ │ └── new.html.heex │ ├── user_reset_password │ │ ├── edit.html.heex │ │ └── new.html.heex │ ├── user_session │ │ └── new.html.heex │ └── user_settings │ │ └── edit.html.heex │ └── views │ ├── error_helpers.ex │ ├── error_view.ex │ ├── layout_view.ex │ ├── page_view.ex │ ├── todo_item_view.ex │ ├── user_confirmation_view.ex │ ├── user_registration_view.ex │ ├── user_reset_password_view.ex │ ├── user_session_view.ex │ └── user_settings_view.ex ├── mix.exs ├── mix.lock ├── priv ├── gettext │ ├── en │ │ └── LC_MESSAGES │ │ │ └── errors.po │ └── errors.pot ├── repo │ ├── migrations │ │ ├── .formatter.exs │ │ └── 20220221061920_create_users_auth_tables.exs │ └── seeds.exs ├── static │ ├── favicon.ico │ ├── images │ │ └── phoenix.png │ └── robots.txt └── user_repo │ └── migrations │ └── 20220221192607_add_todo_list_table.exs └── test ├── sqlite_scale ├── accounts_test.exs └── todo_items_test.exs ├── sqlite_scale_web ├── controllers │ ├── page_controller_test.exs │ ├── todo_item_controller_test.exs │ ├── user_auth_test.exs │ ├── user_confirmation_controller_test.exs │ ├── user_registration_controller_test.exs │ ├── user_reset_password_controller_test.exs │ ├── user_session_controller_test.exs │ └── user_settings_controller_test.exs └── views │ ├── error_view_test.exs │ ├── layout_view_test.exs │ └── page_view_test.exs ├── support ├── channel_case.ex ├── conn_case.ex ├── data_case.ex └── fixtures │ ├── accounts_fixtures.ex │ └── todo_items_fixtures.ex └── test_helper.exs /.formatter.exs: -------------------------------------------------------------------------------- 1 | [ 2 | import_deps: [:ecto, :phoenix], 3 | line_length: 120, 4 | inputs: ["*.{ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{ex,exs}"], 5 | subdirectories: ["priv/*/migrations"] 6 | ] 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # The directory Mix will write compiled artifacts to. 2 | /_build/ 3 | 4 | # If you run "mix test --cover", coverage assets end up here. 5 | /cover/ 6 | 7 | # The directory Mix downloads your dependencies sources to. 8 | /deps/ 9 | 10 | # Where 3rd-party dependencies like ExDoc output generated docs. 11 | /doc/ 12 | 13 | # Ignore .fetch files in case you like to edit your project deps locally. 14 | /.fetch 15 | 16 | # If the VM crashes, it generates a dump, let's ignore it too. 17 | erl_crash.dump 18 | 19 | # Also ignore archive artifacts (built via "mix archive.build"). 20 | *.ez 21 | 22 | # Ignore package tarball (built via "mix hex.build"). 23 | sqlite_scale-*.tar 24 | 25 | # Ignore assets that are produced by build tools. 26 | /priv/static/assets/ 27 | 28 | # Ignore digested assets cache. 29 | /priv/static/cache_manifest.json 30 | 31 | # In case you use Node.js/npm, you want to ignore these. 32 | npm-debug.log 33 | /assets/node_modules/ 34 | 35 | # Database files 36 | *.db 37 | *.db-* 38 | 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SqliteScale 2 | 3 | To start your Phoenix server: 4 | 5 | * Install dependencies with `mix deps.get` 6 | * Create and migrate your database with `mix ecto.setup` 7 | * Start Phoenix endpoint with `mix phx.server` or inside IEx with `iex -S mix phx.server` 8 | 9 | Now you can visit [`localhost:4000`](http://localhost:4000) from your browser. 10 | 11 | Ready to run in production? Please [check our deployment guides](https://hexdocs.pm/phoenix/deployment.html). 12 | 13 | ## Learn more 14 | 15 | * Official website: https://www.phoenixframework.org/ 16 | * Guides: https://hexdocs.pm/phoenix/overview.html 17 | * Docs: https://hexdocs.pm/phoenix 18 | * Forum: https://elixirforum.com/c/phoenix-forum 19 | * Source: https://github.com/phoenixframework/phoenix 20 | -------------------------------------------------------------------------------- /assets/css/app.css: -------------------------------------------------------------------------------- 1 | /* This file is for your main application CSS */ 2 | @import "./phoenix.css"; 3 | 4 | /* Alerts and form errors used by phx.new */ 5 | .alert { 6 | padding: 15px; 7 | margin-bottom: 20px; 8 | border: 1px solid transparent; 9 | border-radius: 4px; 10 | } 11 | .alert-info { 12 | color: #31708f; 13 | background-color: #d9edf7; 14 | border-color: #bce8f1; 15 | } 16 | .alert-warning { 17 | color: #8a6d3b; 18 | background-color: #fcf8e3; 19 | border-color: #faebcc; 20 | } 21 | .alert-danger { 22 | color: #a94442; 23 | background-color: #f2dede; 24 | border-color: #ebccd1; 25 | } 26 | .alert p { 27 | margin-bottom: 0; 28 | } 29 | .alert:empty { 30 | display: none; 31 | } 32 | .invalid-feedback { 33 | color: #a94442; 34 | display: block; 35 | margin: -1rem 0 2rem; 36 | } 37 | 38 | /* LiveView specific classes for your customization */ 39 | .phx-no-feedback.invalid-feedback, 40 | .phx-no-feedback .invalid-feedback { 41 | display: none; 42 | } 43 | 44 | .phx-click-loading { 45 | opacity: 0.5; 46 | transition: opacity 1s ease-out; 47 | } 48 | 49 | .phx-loading{ 50 | cursor: wait; 51 | } 52 | 53 | .phx-modal { 54 | opacity: 1!important; 55 | position: fixed; 56 | z-index: 1; 57 | left: 0; 58 | top: 0; 59 | width: 100%; 60 | height: 100%; 61 | overflow: auto; 62 | background-color: rgba(0,0,0,0.4); 63 | } 64 | 65 | .phx-modal-content { 66 | background-color: #fefefe; 67 | margin: 15vh auto; 68 | padding: 20px; 69 | border: 1px solid #888; 70 | width: 80%; 71 | } 72 | 73 | .phx-modal-close { 74 | color: #aaa; 75 | float: right; 76 | font-size: 28px; 77 | font-weight: bold; 78 | } 79 | 80 | .phx-modal-close:hover, 81 | .phx-modal-close:focus { 82 | color: black; 83 | text-decoration: none; 84 | cursor: pointer; 85 | } 86 | 87 | .fade-in-scale { 88 | animation: 0.2s ease-in 0s normal forwards 1 fade-in-scale-keys; 89 | } 90 | 91 | .fade-out-scale { 92 | animation: 0.2s ease-out 0s normal forwards 1 fade-out-scale-keys; 93 | } 94 | 95 | .fade-in { 96 | animation: 0.2s ease-out 0s normal forwards 1 fade-in-keys; 97 | } 98 | .fade-out { 99 | animation: 0.2s ease-out 0s normal forwards 1 fade-out-keys; 100 | } 101 | 102 | @keyframes fade-in-scale-keys{ 103 | 0% { scale: 0.95; opacity: 0; } 104 | 100% { scale: 1.0; opacity: 1; } 105 | } 106 | 107 | @keyframes fade-out-scale-keys{ 108 | 0% { scale: 1.0; opacity: 1; } 109 | 100% { scale: 0.95; opacity: 0; } 110 | } 111 | 112 | @keyframes fade-in-keys{ 113 | 0% { opacity: 0; } 114 | 100% { opacity: 1; } 115 | } 116 | 117 | @keyframes fade-out-keys{ 118 | 0% { opacity: 1; } 119 | 100% { opacity: 0; } 120 | } 121 | -------------------------------------------------------------------------------- /assets/css/phoenix.css: -------------------------------------------------------------------------------- 1 | /* Includes some default style for the starter application. 2 | * This can be safely deleted to start fresh. 3 | */ 4 | 5 | /* Milligram v1.4.1 https://milligram.github.io 6 | * Copyright (c) 2020 CJ Patoilo Licensed under the MIT license 7 | */ 8 | 9 | *,*:after,*:before{box-sizing:inherit}html{box-sizing:border-box;font-size:62.5%}body{color:#000000;font-family:'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;font-size:1.6em;font-weight:300;letter-spacing:.01em;line-height:1.6}blockquote{border-left:0.3rem solid #d1d1d1;margin-left:0;margin-right:0;padding:1rem 1.5rem}blockquote *:last-child{margin-bottom:0}.button,button,input[type='button'],input[type='reset'],input[type='submit']{background-color:#0069d9;border:0.1rem solid #0069d9;border-radius:.4rem;color:#fff;cursor:pointer;display:inline-block;font-size:1.1rem;font-weight:700;height:3.8rem;letter-spacing:.1rem;line-height:3.8rem;padding:0 3.0rem;text-align:center;text-decoration:none;text-transform:uppercase;white-space:nowrap}.button:focus,.button:hover,button:focus,button:hover,input[type='button']:focus,input[type='button']:hover,input[type='reset']:focus,input[type='reset']:hover,input[type='submit']:focus,input[type='submit']:hover{background-color:#606c76;border-color:#606c76;color:#fff;outline:0}.button[disabled],button[disabled],input[type='button'][disabled],input[type='reset'][disabled],input[type='submit'][disabled]{cursor:default;opacity:.5}.button[disabled]:focus,.button[disabled]:hover,button[disabled]:focus,button[disabled]:hover,input[type='button'][disabled]:focus,input[type='button'][disabled]:hover,input[type='reset'][disabled]:focus,input[type='reset'][disabled]:hover,input[type='submit'][disabled]:focus,input[type='submit'][disabled]:hover{background-color:#0069d9;border-color:#0069d9}.button.button-outline,button.button-outline,input[type='button'].button-outline,input[type='reset'].button-outline,input[type='submit'].button-outline{background-color:transparent;color:#0069d9}.button.button-outline:focus,.button.button-outline:hover,button.button-outline:focus,button.button-outline:hover,input[type='button'].button-outline:focus,input[type='button'].button-outline:hover,input[type='reset'].button-outline:focus,input[type='reset'].button-outline:hover,input[type='submit'].button-outline:focus,input[type='submit'].button-outline:hover{background-color:transparent;border-color:#606c76;color:#606c76}.button.button-outline[disabled]:focus,.button.button-outline[disabled]:hover,button.button-outline[disabled]:focus,button.button-outline[disabled]:hover,input[type='button'].button-outline[disabled]:focus,input[type='button'].button-outline[disabled]:hover,input[type='reset'].button-outline[disabled]:focus,input[type='reset'].button-outline[disabled]:hover,input[type='submit'].button-outline[disabled]:focus,input[type='submit'].button-outline[disabled]:hover{border-color:inherit;color:#0069d9}.button.button-clear,button.button-clear,input[type='button'].button-clear,input[type='reset'].button-clear,input[type='submit'].button-clear{background-color:transparent;border-color:transparent;color:#0069d9}.button.button-clear:focus,.button.button-clear:hover,button.button-clear:focus,button.button-clear:hover,input[type='button'].button-clear:focus,input[type='button'].button-clear:hover,input[type='reset'].button-clear:focus,input[type='reset'].button-clear:hover,input[type='submit'].button-clear:focus,input[type='submit'].button-clear:hover{background-color:transparent;border-color:transparent;color:#606c76}.button.button-clear[disabled]:focus,.button.button-clear[disabled]:hover,button.button-clear[disabled]:focus,button.button-clear[disabled]:hover,input[type='button'].button-clear[disabled]:focus,input[type='button'].button-clear[disabled]:hover,input[type='reset'].button-clear[disabled]:focus,input[type='reset'].button-clear[disabled]:hover,input[type='submit'].button-clear[disabled]:focus,input[type='submit'].button-clear[disabled]:hover{color:#0069d9}code{background:#f4f5f6;border-radius:.4rem;font-size:86%;margin:0 .2rem;padding:.2rem .5rem;white-space:nowrap}pre{background:#f4f5f6;border-left:0.3rem solid #0069d9;overflow-y:hidden}pre>code{border-radius:0;display:block;padding:1rem 1.5rem;white-space:pre}hr{border:0;border-top:0.1rem solid #f4f5f6;margin:3.0rem 0}input[type='color'],input[type='date'],input[type='datetime'],input[type='datetime-local'],input[type='email'],input[type='month'],input[type='number'],input[type='password'],input[type='search'],input[type='tel'],input[type='text'],input[type='url'],input[type='week'],input:not([type]),textarea,select{-webkit-appearance:none;background-color:transparent;border:0.1rem solid #d1d1d1;border-radius:.4rem;box-shadow:none;box-sizing:inherit;height:3.8rem;padding:.6rem 1.0rem .7rem;width:100%}input[type='color']:focus,input[type='date']:focus,input[type='datetime']:focus,input[type='datetime-local']:focus,input[type='email']:focus,input[type='month']:focus,input[type='number']:focus,input[type='password']:focus,input[type='search']:focus,input[type='tel']:focus,input[type='text']:focus,input[type='url']:focus,input[type='week']:focus,input:not([type]):focus,textarea:focus,select:focus{border-color:#0069d9;outline:0}select{background:url('data:image/svg+xml;utf8,') center right no-repeat;padding-right:3.0rem}select:focus{background-image:url('data:image/svg+xml;utf8,')}select[multiple]{background:none;height:auto}textarea{min-height:6.5rem}label,legend{display:block;font-size:1.6rem;font-weight:700;margin-bottom:.5rem}fieldset{border-width:0;padding:0}input[type='checkbox'],input[type='radio']{display:inline}.label-inline{display:inline-block;font-weight:normal;margin-left:.5rem}.container{margin:0 auto;max-width:112.0rem;padding:0 2.0rem;position:relative;width:100%}.row{display:flex;flex-direction:column;padding:0;width:100%}.row.row-no-padding{padding:0}.row.row-no-padding>.column{padding:0}.row.row-wrap{flex-wrap:wrap}.row.row-top{align-items:flex-start}.row.row-bottom{align-items:flex-end}.row.row-center{align-items:center}.row.row-stretch{align-items:stretch}.row.row-baseline{align-items:baseline}.row .column{display:block;flex:1 1 auto;margin-left:0;max-width:100%;width:100%}.row .column.column-offset-10{margin-left:10%}.row .column.column-offset-20{margin-left:20%}.row .column.column-offset-25{margin-left:25%}.row .column.column-offset-33,.row .column.column-offset-34{margin-left:33.3333%}.row .column.column-offset-40{margin-left:40%}.row .column.column-offset-50{margin-left:50%}.row .column.column-offset-60{margin-left:60%}.row .column.column-offset-66,.row .column.column-offset-67{margin-left:66.6666%}.row .column.column-offset-75{margin-left:75%}.row .column.column-offset-80{margin-left:80%}.row .column.column-offset-90{margin-left:90%}.row .column.column-10{flex:0 0 10%;max-width:10%}.row .column.column-20{flex:0 0 20%;max-width:20%}.row .column.column-25{flex:0 0 25%;max-width:25%}.row .column.column-33,.row .column.column-34{flex:0 0 33.3333%;max-width:33.3333%}.row .column.column-40{flex:0 0 40%;max-width:40%}.row .column.column-50{flex:0 0 50%;max-width:50%}.row .column.column-60{flex:0 0 60%;max-width:60%}.row .column.column-66,.row .column.column-67{flex:0 0 66.6666%;max-width:66.6666%}.row .column.column-75{flex:0 0 75%;max-width:75%}.row .column.column-80{flex:0 0 80%;max-width:80%}.row .column.column-90{flex:0 0 90%;max-width:90%}.row .column .column-top{align-self:flex-start}.row .column .column-bottom{align-self:flex-end}.row .column .column-center{align-self:center}@media (min-width: 40rem){.row{flex-direction:row;margin-left:-1.0rem;width:calc(100% + 2.0rem)}.row .column{margin-bottom:inherit;padding:0 1.0rem}}a{color:#0069d9;text-decoration:none}a:focus,a:hover{color:#606c76}dl,ol,ul{list-style:none;margin-top:0;padding-left:0}dl dl,dl ol,dl ul,ol dl,ol ol,ol ul,ul dl,ul ol,ul ul{font-size:90%;margin:1.5rem 0 1.5rem 3.0rem}ol{list-style:decimal inside}ul{list-style:circle inside}.button,button,dd,dt,li{margin-bottom:1.0rem}fieldset,input,select,textarea{margin-bottom:1.5rem}blockquote,dl,figure,form,ol,p,pre,table,ul{margin-bottom:2.5rem}table{border-spacing:0;display:block;overflow-x:auto;text-align:left;width:100%}td,th{border-bottom:0.1rem solid #e1e1e1;padding:1.2rem 1.5rem}td:first-child,th:first-child{padding-left:0}td:last-child,th:last-child{padding-right:0}@media (min-width: 40rem){table{display:table;overflow-x:initial}}b,strong{font-weight:bold}p{margin-top:0}h1,h2,h3,h4,h5,h6{font-weight:300;letter-spacing:-.1rem;margin-bottom:2.0rem;margin-top:0}h1{font-size:4.6rem;line-height:1.2}h2{font-size:3.6rem;line-height:1.25}h3{font-size:2.8rem;line-height:1.3}h4{font-size:2.2rem;letter-spacing:-.08rem;line-height:1.35}h5{font-size:1.8rem;letter-spacing:-.05rem;line-height:1.5}h6{font-size:1.6rem;letter-spacing:0;line-height:1.4}img{max-width:100%}.clearfix:after{clear:both;content:' ';display:table}.float-left{float:left}.float-right{float:right} 10 | 11 | /* General style */ 12 | h1{font-size: 3.6rem; line-height: 1.25} 13 | h2{font-size: 2.8rem; line-height: 1.3} 14 | h3{font-size: 2.2rem; letter-spacing: -.08rem; line-height: 1.35} 15 | h4{font-size: 1.8rem; letter-spacing: -.05rem; line-height: 1.5} 16 | h5{font-size: 1.6rem; letter-spacing: 0; line-height: 1.4} 17 | h6{font-size: 1.4rem; letter-spacing: 0; line-height: 1.2} 18 | pre{padding: 1em;} 19 | 20 | .container{ 21 | margin: 0 auto; 22 | max-width: 80.0rem; 23 | padding: 0 2.0rem; 24 | position: relative; 25 | width: 100% 26 | } 27 | select { 28 | width: auto; 29 | } 30 | 31 | /* Phoenix promo and logo */ 32 | .phx-hero { 33 | text-align: center; 34 | border-bottom: 1px solid #e3e3e3; 35 | background: #eee; 36 | border-radius: 6px; 37 | padding: 3em 3em 1em; 38 | margin-bottom: 3rem; 39 | font-weight: 200; 40 | font-size: 120%; 41 | } 42 | .phx-hero input { 43 | background: #ffffff; 44 | } 45 | .phx-logo { 46 | min-width: 300px; 47 | margin: 1rem; 48 | display: block; 49 | } 50 | .phx-logo img { 51 | width: auto; 52 | display: block; 53 | } 54 | 55 | /* Headers */ 56 | header { 57 | width: 100%; 58 | background: #fdfdfd; 59 | border-bottom: 1px solid #eaeaea; 60 | margin-bottom: 2rem; 61 | } 62 | header section { 63 | align-items: center; 64 | display: flex; 65 | flex-direction: column; 66 | justify-content: space-between; 67 | } 68 | header section :first-child { 69 | order: 2; 70 | } 71 | header section :last-child { 72 | order: 1; 73 | } 74 | header nav ul, 75 | header nav li { 76 | margin: 0; 77 | padding: 0; 78 | display: block; 79 | text-align: right; 80 | white-space: nowrap; 81 | } 82 | header nav ul { 83 | margin: 1rem; 84 | margin-top: 0; 85 | } 86 | header nav a { 87 | display: block; 88 | } 89 | 90 | @media (min-width: 40.0rem) { /* Small devices (landscape phones, 576px and up) */ 91 | header section { 92 | flex-direction: row; 93 | } 94 | header nav ul { 95 | margin: 1rem; 96 | } 97 | .phx-logo { 98 | flex-basis: 527px; 99 | margin: 2rem 1rem; 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /assets/js/app.js: -------------------------------------------------------------------------------- 1 | // We import the CSS which is extracted to its own file by esbuild. 2 | // Remove this line if you add a your own CSS build pipeline (e.g postcss). 3 | import "../css/app.css" 4 | 5 | // If you want to use Phoenix channels, run `mix help phx.gen.channel` 6 | // to get started and then uncomment the line below. 7 | // import "./user_socket.js" 8 | 9 | // You can include dependencies in two ways. 10 | // 11 | // The simplest option is to put them in assets/vendor and 12 | // import them using relative paths: 13 | // 14 | // import "../vendor/some-package.js" 15 | // 16 | // Alternatively, you can `npm install some-package --prefix assets` and import 17 | // them using a path starting with the package name: 18 | // 19 | // import "some-package" 20 | // 21 | 22 | // Include phoenix_html to handle method=PUT/DELETE in forms and buttons. 23 | import "phoenix_html" 24 | // Establish Phoenix Socket and LiveView configuration. 25 | import {Socket} from "phoenix" 26 | import {LiveSocket} from "phoenix_live_view" 27 | import topbar from "../vendor/topbar" 28 | 29 | let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content") 30 | let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}}) 31 | 32 | // Show progress bar on live navigation and form submits 33 | topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"}) 34 | window.addEventListener("phx:page-loading-start", info => topbar.show()) 35 | window.addEventListener("phx:page-loading-stop", info => topbar.hide()) 36 | 37 | // connect if there are any LiveViews on the page 38 | liveSocket.connect() 39 | 40 | // expose liveSocket on window for web console debug logs and latency simulation: 41 | // >> liveSocket.enableDebug() 42 | // >> liveSocket.enableLatencySim(1000) // enabled for duration of browser session 43 | // >> liveSocket.disableLatencySim() 44 | window.liveSocket = liveSocket 45 | 46 | -------------------------------------------------------------------------------- /assets/vendor/topbar.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @license MIT 3 | * topbar 1.0.0, 2021-01-06 4 | * https://buunguyen.github.io/topbar 5 | * Copyright (c) 2021 Buu Nguyen 6 | */ 7 | (function (window, document) { 8 | "use strict"; 9 | 10 | // https://gist.github.com/paulirish/1579671 11 | (function () { 12 | var lastTime = 0; 13 | var vendors = ["ms", "moz", "webkit", "o"]; 14 | for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { 15 | window.requestAnimationFrame = 16 | window[vendors[x] + "RequestAnimationFrame"]; 17 | window.cancelAnimationFrame = 18 | window[vendors[x] + "CancelAnimationFrame"] || 19 | window[vendors[x] + "CancelRequestAnimationFrame"]; 20 | } 21 | if (!window.requestAnimationFrame) 22 | window.requestAnimationFrame = function (callback, element) { 23 | var currTime = new Date().getTime(); 24 | var timeToCall = Math.max(0, 16 - (currTime - lastTime)); 25 | var id = window.setTimeout(function () { 26 | callback(currTime + timeToCall); 27 | }, timeToCall); 28 | lastTime = currTime + timeToCall; 29 | return id; 30 | }; 31 | if (!window.cancelAnimationFrame) 32 | window.cancelAnimationFrame = function (id) { 33 | clearTimeout(id); 34 | }; 35 | })(); 36 | 37 | var canvas, 38 | progressTimerId, 39 | fadeTimerId, 40 | currentProgress, 41 | showing, 42 | addEvent = function (elem, type, handler) { 43 | if (elem.addEventListener) elem.addEventListener(type, handler, false); 44 | else if (elem.attachEvent) elem.attachEvent("on" + type, handler); 45 | else elem["on" + type] = handler; 46 | }, 47 | options = { 48 | autoRun: true, 49 | barThickness: 3, 50 | barColors: { 51 | 0: "rgba(26, 188, 156, .9)", 52 | ".25": "rgba(52, 152, 219, .9)", 53 | ".50": "rgba(241, 196, 15, .9)", 54 | ".75": "rgba(230, 126, 34, .9)", 55 | "1.0": "rgba(211, 84, 0, .9)", 56 | }, 57 | shadowBlur: 10, 58 | shadowColor: "rgba(0, 0, 0, .6)", 59 | className: null, 60 | }, 61 | repaint = function () { 62 | canvas.width = window.innerWidth; 63 | canvas.height = options.barThickness * 5; // need space for shadow 64 | 65 | var ctx = canvas.getContext("2d"); 66 | ctx.shadowBlur = options.shadowBlur; 67 | ctx.shadowColor = options.shadowColor; 68 | 69 | var lineGradient = ctx.createLinearGradient(0, 0, canvas.width, 0); 70 | for (var stop in options.barColors) 71 | lineGradient.addColorStop(stop, options.barColors[stop]); 72 | ctx.lineWidth = options.barThickness; 73 | ctx.beginPath(); 74 | ctx.moveTo(0, options.barThickness / 2); 75 | ctx.lineTo( 76 | Math.ceil(currentProgress * canvas.width), 77 | options.barThickness / 2 78 | ); 79 | ctx.strokeStyle = lineGradient; 80 | ctx.stroke(); 81 | }, 82 | createCanvas = function () { 83 | canvas = document.createElement("canvas"); 84 | var style = canvas.style; 85 | style.position = "fixed"; 86 | style.top = style.left = style.right = style.margin = style.padding = 0; 87 | style.zIndex = 100001; 88 | style.display = "none"; 89 | if (options.className) canvas.classList.add(options.className); 90 | document.body.appendChild(canvas); 91 | addEvent(window, "resize", repaint); 92 | }, 93 | topbar = { 94 | config: function (opts) { 95 | for (var key in opts) 96 | if (options.hasOwnProperty(key)) options[key] = opts[key]; 97 | }, 98 | show: function () { 99 | if (showing) return; 100 | showing = true; 101 | if (fadeTimerId !== null) window.cancelAnimationFrame(fadeTimerId); 102 | if (!canvas) createCanvas(); 103 | canvas.style.opacity = 1; 104 | canvas.style.display = "block"; 105 | topbar.progress(0); 106 | if (options.autoRun) { 107 | (function loop() { 108 | progressTimerId = window.requestAnimationFrame(loop); 109 | topbar.progress( 110 | "+" + 0.05 * Math.pow(1 - Math.sqrt(currentProgress), 2) 111 | ); 112 | })(); 113 | } 114 | }, 115 | progress: function (to) { 116 | if (typeof to === "undefined") return currentProgress; 117 | if (typeof to === "string") { 118 | to = 119 | (to.indexOf("+") >= 0 || to.indexOf("-") >= 0 120 | ? currentProgress 121 | : 0) + parseFloat(to); 122 | } 123 | currentProgress = to > 1 ? 1 : to; 124 | repaint(); 125 | return currentProgress; 126 | }, 127 | hide: function () { 128 | if (!showing) return; 129 | showing = false; 130 | if (progressTimerId != null) { 131 | window.cancelAnimationFrame(progressTimerId); 132 | progressTimerId = null; 133 | } 134 | (function loop() { 135 | if (topbar.progress("+.1") >= 1) { 136 | canvas.style.opacity -= 0.05; 137 | if (canvas.style.opacity <= 0.05) { 138 | canvas.style.display = "none"; 139 | fadeTimerId = null; 140 | return; 141 | } 142 | } 143 | fadeTimerId = window.requestAnimationFrame(loop); 144 | })(); 145 | }, 146 | }; 147 | 148 | if (typeof module === "object" && typeof module.exports === "object") { 149 | module.exports = topbar; 150 | } else if (typeof define === "function" && define.amd) { 151 | define(function () { 152 | return topbar; 153 | }); 154 | } else { 155 | this.topbar = topbar; 156 | } 157 | }.call(this, window, document)); 158 | -------------------------------------------------------------------------------- /config/config.exs: -------------------------------------------------------------------------------- 1 | # This file is responsible for configuring your application 2 | # and its dependencies with the aid of the Config module. 3 | # 4 | # This configuration file is loaded before any dependency and 5 | # is restricted to this project. 6 | 7 | # General application configuration 8 | import Config 9 | 10 | config :sqlite_scale, 11 | ecto_repos: [SqliteScale.Repo], 12 | generators: [binary_id: true] 13 | 14 | # Configures the endpoint 15 | config :sqlite_scale, SqliteScaleWeb.Endpoint, 16 | url: [host: "localhost"], 17 | render_errors: [view: SqliteScaleWeb.ErrorView, accepts: ~w(html json), layout: false], 18 | pubsub_server: SqliteScale.PubSub, 19 | live_view: [signing_salt: "xDOGAAu1"] 20 | 21 | # Configures the mailer 22 | # 23 | # By default it uses the "Local" adapter which stores the emails 24 | # locally. You can see the emails in your browser, at "/dev/mailbox". 25 | # 26 | # For production it's recommended to configure a different adapter 27 | # at the `config/runtime.exs`. 28 | config :sqlite_scale, SqliteScale.Mailer, adapter: Swoosh.Adapters.Local 29 | 30 | # Swoosh API client is needed for adapters other than SMTP. 31 | config :swoosh, :api_client, false 32 | 33 | # Configure esbuild (the version is required) 34 | config :esbuild, 35 | version: "0.14.0", 36 | default: [ 37 | args: 38 | ~w(js/app.js --bundle --target=es2017 --outdir=../priv/static/assets --external:/fonts/* --external:/images/*), 39 | cd: Path.expand("../assets", __DIR__), 40 | env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)} 41 | ] 42 | 43 | # Configures Elixir's Logger 44 | config :logger, :console, 45 | format: "$time $metadata[$level] $message\n", 46 | metadata: [:request_id] 47 | 48 | # Use Jason for JSON parsing in Phoenix 49 | config :phoenix, :json_library, Jason 50 | 51 | # Import environment specific config. This must remain at the bottom 52 | # of this file so it overrides the configuration defined above. 53 | import_config "#{config_env()}.exs" 54 | -------------------------------------------------------------------------------- /config/dev.exs: -------------------------------------------------------------------------------- 1 | import Config 2 | 3 | # Configure your database 4 | config :sqlite_scale, SqliteScale.Repo, 5 | database: Path.expand("../sqlite_scale_dev.db", Path.dirname(__ENV__.file)), 6 | pool_size: 5, 7 | show_sensitive_data_on_connection_error: true 8 | 9 | # For development, we disable any cache and enable 10 | # debugging and code reloading. 11 | # 12 | # The watchers configuration can be used to run external 13 | # watchers to your application. For example, we use it 14 | # with esbuild to bundle .js and .css sources. 15 | config :sqlite_scale, SqliteScaleWeb.Endpoint, 16 | # Binding to loopback ipv4 address prevents access from other machines. 17 | # Change to `ip: {0, 0, 0, 0}` to allow access from other machines. 18 | http: [ip: {127, 0, 0, 1}, port: 4000], 19 | check_origin: false, 20 | code_reloader: true, 21 | debug_errors: true, 22 | secret_key_base: "8DrS/TWI0/+eEjZdgdcFo/3qDyWe2/i4dHHnbFsm+hWfbSkfuEAjeFBXQtZNxB1h", 23 | watchers: [ 24 | # Start the esbuild watcher by calling Esbuild.install_and_run(:default, args) 25 | esbuild: {Esbuild, :install_and_run, [:default, ~w(--sourcemap=inline --watch)]} 26 | ] 27 | 28 | # ## SSL Support 29 | # 30 | # In order to use HTTPS in development, a self-signed 31 | # certificate can be generated by running the following 32 | # Mix task: 33 | # 34 | # mix phx.gen.cert 35 | # 36 | # Note that this task requires Erlang/OTP 20 or later. 37 | # Run `mix help phx.gen.cert` for more information. 38 | # 39 | # The `http:` config above can be replaced with: 40 | # 41 | # https: [ 42 | # port: 4001, 43 | # cipher_suite: :strong, 44 | # keyfile: "priv/cert/selfsigned_key.pem", 45 | # certfile: "priv/cert/selfsigned.pem" 46 | # ], 47 | # 48 | # If desired, both `http:` and `https:` keys can be 49 | # configured to run both http and https servers on 50 | # different ports. 51 | 52 | # Watch static and templates for browser reloading. 53 | config :sqlite_scale, SqliteScaleWeb.Endpoint, 54 | live_reload: [ 55 | patterns: [ 56 | ~r"priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$", 57 | ~r"priv/gettext/.*(po)$", 58 | ~r"lib/sqlite_scale_web/(live|views)/.*(ex)$", 59 | ~r"lib/sqlite_scale_web/templates/.*(eex)$" 60 | ] 61 | ] 62 | 63 | # Do not include metadata nor timestamps in development logs 64 | config :logger, :console, format: "[$level] $message\n" 65 | 66 | # Set a higher stacktrace during development. Avoid configuring such 67 | # in production as building large stacktraces may be expensive. 68 | config :phoenix, :stacktrace_depth, 20 69 | 70 | # Initialize plugs at runtime for faster development compilation 71 | config :phoenix, :plug_init_mode, :runtime 72 | -------------------------------------------------------------------------------- /config/prod.exs: -------------------------------------------------------------------------------- 1 | import Config 2 | 3 | # For production, don't forget to configure the url host 4 | # to something meaningful, Phoenix uses this information 5 | # when generating URLs. 6 | # 7 | # Note we also include the path to a cache manifest 8 | # containing the digested version of static files. This 9 | # manifest is generated by the `mix phx.digest` task, 10 | # which you should run after static files are built and 11 | # before starting your production server. 12 | config :sqlite_scale, SqliteScaleWeb.Endpoint, cache_static_manifest: "priv/static/cache_manifest.json" 13 | 14 | # Do not print debug messages in production 15 | config :logger, level: :info 16 | 17 | # ## SSL Support 18 | # 19 | # To get SSL working, you will need to add the `https` key 20 | # to the previous section and set your `:url` port to 443: 21 | # 22 | # config :sqlite_scale, SqliteScaleWeb.Endpoint, 23 | # ..., 24 | # url: [host: "example.com", port: 443], 25 | # https: [ 26 | # ..., 27 | # port: 443, 28 | # cipher_suite: :strong, 29 | # keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"), 30 | # certfile: System.get_env("SOME_APP_SSL_CERT_PATH") 31 | # ] 32 | # 33 | # The `cipher_suite` is set to `:strong` to support only the 34 | # latest and more secure SSL ciphers. This means old browsers 35 | # and clients may not be supported. You can set it to 36 | # `:compatible` for wider support. 37 | # 38 | # `:keyfile` and `:certfile` expect an absolute path to the key 39 | # and cert in disk or a relative path inside priv, for example 40 | # "priv/ssl/server.key". For all supported SSL configuration 41 | # options, see https://hexdocs.pm/plug/Plug.SSL.html#configure/1 42 | # 43 | # We also recommend setting `force_ssl` in your endpoint, ensuring 44 | # no data is ever sent via http, always redirecting to https: 45 | # 46 | # config :sqlite_scale, SqliteScaleWeb.Endpoint, 47 | # force_ssl: [hsts: true] 48 | # 49 | # Check `Plug.SSL` for all available options in `force_ssl`. 50 | -------------------------------------------------------------------------------- /config/runtime.exs: -------------------------------------------------------------------------------- 1 | import Config 2 | 3 | # config/runtime.exs is executed for all environments, including 4 | # during releases. It is executed after compilation and before the 5 | # system starts, so it is typically used to load production configuration 6 | # and secrets from environment variables or elsewhere. Do not define 7 | # any compile-time configuration in here, as it won't be applied. 8 | # The block below contains prod specific runtime configuration. 9 | 10 | # Start the phoenix server if environment is set and running in a release 11 | if System.get_env("PHX_SERVER") && System.get_env("RELEASE_NAME") do 12 | config :sqlite_scale, SqliteScaleWeb.Endpoint, server: true 13 | end 14 | 15 | if config_env() == :prod do 16 | database_path = 17 | System.get_env("DATABASE_PATH") || 18 | raise """ 19 | environment variable DATABASE_PATH is missing. 20 | For example: /etc/sqlite_scale/sqlite_scale.db 21 | """ 22 | 23 | config :sqlite_scale, SqliteScale.Repo, 24 | database: database_path, 25 | pool_size: String.to_integer(System.get_env("POOL_SIZE") || "5") 26 | 27 | # The secret key base is used to sign/encrypt cookies and other secrets. 28 | # A default value is used in config/dev.exs and config/test.exs but you 29 | # want to use a different value for prod and you most likely don't want 30 | # to check this value into version control, so we use an environment 31 | # variable instead. 32 | secret_key_base = 33 | System.get_env("SECRET_KEY_BASE") || 34 | raise """ 35 | environment variable SECRET_KEY_BASE is missing. 36 | You can generate one by calling: mix phx.gen.secret 37 | """ 38 | 39 | host = System.get_env("PHX_HOST") || "example.com" 40 | port = String.to_integer(System.get_env("PORT") || "4000") 41 | 42 | config :sqlite_scale, SqliteScaleWeb.Endpoint, 43 | url: [host: host, port: 443], 44 | http: [ 45 | # Enable IPv6 and bind on all interfaces. 46 | # Set it to {0, 0, 0, 0, 0, 0, 0, 1} for local network only access. 47 | # See the documentation on https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html 48 | # for details about using IPv6 vs IPv4 and loopback vs public addresses. 49 | ip: {0, 0, 0, 0, 0, 0, 0, 0}, 50 | port: port 51 | ], 52 | secret_key_base: secret_key_base 53 | 54 | # ## Using releases 55 | # 56 | # If you are doing OTP releases, you need to instruct Phoenix 57 | # to start each relevant endpoint: 58 | # 59 | # config :sqlite_scale, SqliteScaleWeb.Endpoint, server: true 60 | # 61 | # Then you can assemble a release by calling `mix release`. 62 | # See `mix help release` for more information. 63 | 64 | # ## Configuring the mailer 65 | # 66 | # In production you need to configure the mailer to use a different adapter. 67 | # Also, you may need to configure the Swoosh API client of your choice if you 68 | # are not using SMTP. Here is an example of the configuration: 69 | # 70 | # config :sqlite_scale, SqliteScale.Mailer, 71 | # adapter: Swoosh.Adapters.Mailgun, 72 | # api_key: System.get_env("MAILGUN_API_KEY"), 73 | # domain: System.get_env("MAILGUN_DOMAIN") 74 | # 75 | # For this example you need include a HTTP client required by Swoosh API client. 76 | # Swoosh supports Hackney and Finch out of the box: 77 | # 78 | # config :swoosh, :api_client, Swoosh.ApiClient.Hackney 79 | # 80 | # See https://hexdocs.pm/swoosh/Swoosh.html#module-installation for details. 81 | end 82 | -------------------------------------------------------------------------------- /config/test.exs: -------------------------------------------------------------------------------- 1 | import Config 2 | 3 | # Only in tests, remove the complexity from the password hashing algorithm 4 | config :bcrypt_elixir, :log_rounds, 1 5 | 6 | # Configure your database 7 | # 8 | # The MIX_TEST_PARTITION environment variable can be used 9 | # to provide built-in test partitioning in CI environment. 10 | # Run `mix help test` for more information. 11 | config :sqlite_scale, SqliteScale.Repo, 12 | database: Path.expand("../sqlite_scale_test.db", Path.dirname(__ENV__.file)), 13 | pool_size: 5, 14 | pool: Ecto.Adapters.SQL.Sandbox 15 | 16 | # We don't run a server during test. If one is required, 17 | # you can enable the server option below. 18 | config :sqlite_scale, SqliteScaleWeb.Endpoint, 19 | http: [ip: {127, 0, 0, 1}, port: 4002], 20 | secret_key_base: "sNdZ77z6pLPXUxggxPytffLPO7FcTTVeGGwYatqaEGBzaUDB1QZNimxgGyc9IYWu", 21 | server: false 22 | 23 | # In test we don't send emails. 24 | config :sqlite_scale, SqliteScale.Mailer, adapter: Swoosh.Adapters.Test 25 | 26 | # Print only warnings and errors during test 27 | config :logger, level: :warn 28 | 29 | # Initialize plugs at runtime for faster test compilation 30 | config :phoenix, :plug_init_mode, :runtime 31 | -------------------------------------------------------------------------------- /lib/sqlite_scale.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale do 2 | @moduledoc """ 3 | SqliteScale keeps the contexts that define your domain 4 | and business logic. 5 | 6 | Contexts are also responsible for managing your data, regardless 7 | if it comes from the database, an external API or others. 8 | """ 9 | end 10 | -------------------------------------------------------------------------------- /lib/sqlite_scale/accounts.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.Accounts do 2 | @moduledoc """ 3 | The Accounts context. 4 | """ 5 | 6 | import Ecto.Query, warn: false 7 | 8 | alias SqliteScale.Accounts.{User, UserToken, UserNotifier} 9 | alias SqliteScale.DynamicRepoSupervisor.RepoSupervisor 10 | alias SqliteScale.Repo 11 | 12 | ## Database getters 13 | 14 | @doc """ 15 | Gets a user by email. 16 | 17 | ## Examples 18 | 19 | iex> get_user_by_email("foo@example.com") 20 | %User{} 21 | 22 | iex> get_user_by_email("unknown@example.com") 23 | nil 24 | 25 | """ 26 | def get_user_by_email(email) when is_binary(email) do 27 | Repo.get_by(User, email: email) 28 | end 29 | 30 | def list_users do 31 | Repo.all(User) 32 | end 33 | 34 | @doc """ 35 | Gets a user by email and password. 36 | 37 | ## Examples 38 | 39 | iex> get_user_by_email_and_password("foo@example.com", "correct_password") 40 | %User{} 41 | 42 | iex> get_user_by_email_and_password("foo@example.com", "invalid_password") 43 | nil 44 | 45 | """ 46 | def get_user_by_email_and_password(email, password) 47 | when is_binary(email) and is_binary(password) do 48 | user = Repo.get_by(User, email: email) 49 | if User.valid_password?(user, password), do: user 50 | end 51 | 52 | @doc """ 53 | Gets a single user. 54 | 55 | Raises `Ecto.NoResultsError` if the User does not exist. 56 | 57 | ## Examples 58 | 59 | iex> get_user!(123) 60 | %User{} 61 | 62 | iex> get_user!(456) 63 | ** (Ecto.NoResultsError) 64 | 65 | """ 66 | def get_user!(id), do: Repo.get!(User, id) 67 | 68 | ## User registration 69 | 70 | @doc """ 71 | Registers a user. 72 | 73 | ## Examples 74 | 75 | iex> register_user(%{field: value}) 76 | {:ok, %User{}} 77 | 78 | iex> register_user(%{field: bad_value}) 79 | {:error, %Ecto.Changeset{}} 80 | 81 | """ 82 | def register_user(attrs) do 83 | %User{} 84 | |> User.registration_changeset(attrs) 85 | |> Repo.insert() 86 | |> case do 87 | {:ok, %User{} = user} = result -> 88 | RepoSupervisor.add_repo_to_supervisor(user) 89 | result 90 | 91 | error -> 92 | error 93 | end 94 | end 95 | 96 | @doc """ 97 | Returns an `%Ecto.Changeset{}` for tracking user changes. 98 | 99 | ## Examples 100 | 101 | iex> change_user_registration(user) 102 | %Ecto.Changeset{data: %User{}} 103 | 104 | """ 105 | def change_user_registration(%User{} = user, attrs \\ %{}) do 106 | User.registration_changeset(user, attrs, hash_password: false) 107 | end 108 | 109 | ## Settings 110 | 111 | @doc """ 112 | Returns an `%Ecto.Changeset{}` for changing the user email. 113 | 114 | ## Examples 115 | 116 | iex> change_user_email(user) 117 | %Ecto.Changeset{data: %User{}} 118 | 119 | """ 120 | def change_user_email(user, attrs \\ %{}) do 121 | User.email_changeset(user, attrs) 122 | end 123 | 124 | @doc """ 125 | Emulates that the email will change without actually changing 126 | it in the database. 127 | 128 | ## Examples 129 | 130 | iex> apply_user_email(user, "valid password", %{email: ...}) 131 | {:ok, %User{}} 132 | 133 | iex> apply_user_email(user, "invalid password", %{email: ...}) 134 | {:error, %Ecto.Changeset{}} 135 | 136 | """ 137 | def apply_user_email(user, password, attrs) do 138 | user 139 | |> User.email_changeset(attrs) 140 | |> User.validate_current_password(password) 141 | |> Ecto.Changeset.apply_action(:update) 142 | end 143 | 144 | @doc """ 145 | Updates the user email using the given token. 146 | 147 | If the token matches, the user email is updated and the token is deleted. 148 | The confirmed_at date is also updated to the current time. 149 | """ 150 | def update_user_email(user, token) do 151 | context = "change:#{user.email}" 152 | 153 | with {:ok, query} <- UserToken.verify_change_email_token_query(token, context), 154 | %UserToken{sent_to: email} <- Repo.one(query), 155 | {:ok, _} <- Repo.transaction(user_email_multi(user, email, context)) do 156 | :ok 157 | else 158 | _ -> :error 159 | end 160 | end 161 | 162 | defp user_email_multi(user, email, context) do 163 | changeset = 164 | user 165 | |> User.email_changeset(%{email: email}) 166 | |> User.confirm_changeset() 167 | 168 | Ecto.Multi.new() 169 | |> Ecto.Multi.update(:user, changeset) 170 | |> Ecto.Multi.delete_all(:tokens, UserToken.user_and_contexts_query(user, [context])) 171 | end 172 | 173 | @doc """ 174 | Delivers the update email instructions to the given user. 175 | 176 | ## Examples 177 | 178 | iex> deliver_update_email_instructions(user, current_email, &Routes.user_update_email_url(conn, :edit, &1)) 179 | {:ok, %{to: ..., body: ...}} 180 | 181 | """ 182 | def deliver_update_email_instructions(%User{} = user, current_email, update_email_url_fun) 183 | when is_function(update_email_url_fun, 1) do 184 | {encoded_token, user_token} = UserToken.build_email_token(user, "change:#{current_email}") 185 | 186 | Repo.insert!(user_token) 187 | UserNotifier.deliver_update_email_instructions(user, update_email_url_fun.(encoded_token)) 188 | end 189 | 190 | @doc """ 191 | Returns an `%Ecto.Changeset{}` for changing the user password. 192 | 193 | ## Examples 194 | 195 | iex> change_user_password(user) 196 | %Ecto.Changeset{data: %User{}} 197 | 198 | """ 199 | def change_user_password(user, attrs \\ %{}) do 200 | User.password_changeset(user, attrs, hash_password: false) 201 | end 202 | 203 | @doc """ 204 | Updates the user password. 205 | 206 | ## Examples 207 | 208 | iex> update_user_password(user, "valid password", %{password: ...}) 209 | {:ok, %User{}} 210 | 211 | iex> update_user_password(user, "invalid password", %{password: ...}) 212 | {:error, %Ecto.Changeset{}} 213 | 214 | """ 215 | def update_user_password(user, password, attrs) do 216 | changeset = 217 | user 218 | |> User.password_changeset(attrs) 219 | |> User.validate_current_password(password) 220 | 221 | Ecto.Multi.new() 222 | |> Ecto.Multi.update(:user, changeset) 223 | |> Ecto.Multi.delete_all(:tokens, UserToken.user_and_contexts_query(user, :all)) 224 | |> Repo.transaction() 225 | |> case do 226 | {:ok, %{user: user}} -> {:ok, user} 227 | {:error, :user, changeset, _} -> {:error, changeset} 228 | end 229 | end 230 | 231 | ## Session 232 | 233 | @doc """ 234 | Generates a session token. 235 | """ 236 | def generate_user_session_token(user) do 237 | {token, user_token} = UserToken.build_session_token(user) 238 | Repo.insert!(user_token) 239 | token 240 | end 241 | 242 | @doc """ 243 | Gets the user with the given signed token. 244 | """ 245 | def get_user_by_session_token(token) do 246 | {:ok, query} = UserToken.verify_session_token_query(token) 247 | Repo.one(query) 248 | end 249 | 250 | @doc """ 251 | Deletes the signed token with the given context. 252 | """ 253 | def delete_session_token(token) do 254 | Repo.delete_all(UserToken.token_and_context_query(token, "session")) 255 | :ok 256 | end 257 | 258 | ## Confirmation 259 | 260 | @doc """ 261 | Delivers the confirmation email instructions to the given user. 262 | 263 | ## Examples 264 | 265 | iex> deliver_user_confirmation_instructions(user, &Routes.user_confirmation_url(conn, :edit, &1)) 266 | {:ok, %{to: ..., body: ...}} 267 | 268 | iex> deliver_user_confirmation_instructions(confirmed_user, &Routes.user_confirmation_url(conn, :edit, &1)) 269 | {:error, :already_confirmed} 270 | 271 | """ 272 | def deliver_user_confirmation_instructions(%User{} = user, confirmation_url_fun) 273 | when is_function(confirmation_url_fun, 1) do 274 | if user.confirmed_at do 275 | {:error, :already_confirmed} 276 | else 277 | {encoded_token, user_token} = UserToken.build_email_token(user, "confirm") 278 | Repo.insert!(user_token) 279 | UserNotifier.deliver_confirmation_instructions(user, confirmation_url_fun.(encoded_token)) 280 | end 281 | end 282 | 283 | @doc """ 284 | Confirms a user by the given token. 285 | 286 | If the token matches, the user account is marked as confirmed 287 | and the token is deleted. 288 | """ 289 | def confirm_user(token) do 290 | with {:ok, query} <- UserToken.verify_email_token_query(token, "confirm"), 291 | %User{} = user <- Repo.one(query), 292 | {:ok, %{user: user}} <- Repo.transaction(confirm_user_multi(user)) do 293 | {:ok, user} 294 | else 295 | _ -> :error 296 | end 297 | end 298 | 299 | defp confirm_user_multi(user) do 300 | Ecto.Multi.new() 301 | |> Ecto.Multi.update(:user, User.confirm_changeset(user)) 302 | |> Ecto.Multi.delete_all(:tokens, UserToken.user_and_contexts_query(user, ["confirm"])) 303 | end 304 | 305 | ## Reset password 306 | 307 | @doc """ 308 | Delivers the reset password email to the given user. 309 | 310 | ## Examples 311 | 312 | iex> deliver_user_reset_password_instructions(user, &Routes.user_reset_password_url(conn, :edit, &1)) 313 | {:ok, %{to: ..., body: ...}} 314 | 315 | """ 316 | def deliver_user_reset_password_instructions(%User{} = user, reset_password_url_fun) 317 | when is_function(reset_password_url_fun, 1) do 318 | {encoded_token, user_token} = UserToken.build_email_token(user, "reset_password") 319 | Repo.insert!(user_token) 320 | UserNotifier.deliver_reset_password_instructions(user, reset_password_url_fun.(encoded_token)) 321 | end 322 | 323 | @doc """ 324 | Gets the user by reset password token. 325 | 326 | ## Examples 327 | 328 | iex> get_user_by_reset_password_token("validtoken") 329 | %User{} 330 | 331 | iex> get_user_by_reset_password_token("invalidtoken") 332 | nil 333 | 334 | """ 335 | def get_user_by_reset_password_token(token) do 336 | with {:ok, query} <- UserToken.verify_email_token_query(token, "reset_password"), 337 | %User{} = user <- Repo.one(query) do 338 | user 339 | else 340 | _ -> nil 341 | end 342 | end 343 | 344 | @doc """ 345 | Resets the user password. 346 | 347 | ## Examples 348 | 349 | iex> reset_user_password(user, %{password: "new long password", password_confirmation: "new long password"}) 350 | {:ok, %User{}} 351 | 352 | iex> reset_user_password(user, %{password: "valid", password_confirmation: "not the same"}) 353 | {:error, %Ecto.Changeset{}} 354 | 355 | """ 356 | def reset_user_password(user, attrs) do 357 | Ecto.Multi.new() 358 | |> Ecto.Multi.update(:user, User.password_changeset(user, attrs)) 359 | |> Ecto.Multi.delete_all(:tokens, UserToken.user_and_contexts_query(user, :all)) 360 | |> Repo.transaction() 361 | |> case do 362 | {:ok, %{user: user}} -> {:ok, user} 363 | {:error, :user, changeset, _} -> {:error, changeset} 364 | end 365 | end 366 | end 367 | -------------------------------------------------------------------------------- /lib/sqlite_scale/accounts/user.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.Accounts.User do 2 | @moduledoc """ 3 | The user Ecto schema 4 | """ 5 | 6 | use Ecto.Schema 7 | 8 | import Ecto.Changeset 9 | 10 | @primary_key {:id, :binary_id, autogenerate: true} 11 | @foreign_key_type :binary_id 12 | schema "users" do 13 | field :email, :string 14 | field :password, :string, virtual: true, redact: true 15 | field :hashed_password, :string, redact: true 16 | field :confirmed_at, :naive_datetime 17 | 18 | timestamps() 19 | end 20 | 21 | @doc """ 22 | A user changeset for registration. 23 | 24 | It is important to validate the length of both email and password. 25 | Otherwise databases may truncate the email without warnings, which 26 | could lead to unpredictable or insecure behaviour. Long passwords may 27 | also be very expensive to hash for certain algorithms. 28 | 29 | ## Options 30 | 31 | * `:hash_password` - Hashes the password so it can be stored securely 32 | in the database and ensures the password field is cleared to prevent 33 | leaks in the logs. If password hashing is not needed and clearing the 34 | password field is not desired (like when using this changeset for 35 | validations on a LiveView form), this option can be set to `false`. 36 | Defaults to `true`. 37 | """ 38 | def registration_changeset(user, attrs, opts \\ []) do 39 | user 40 | |> cast(attrs, [:email, :password]) 41 | |> validate_email() 42 | |> validate_password(opts) 43 | end 44 | 45 | defp validate_email(changeset) do 46 | changeset 47 | |> validate_required([:email]) 48 | |> validate_format(:email, ~r/^[^\s]+@[^\s]+$/, message: "must have the @ sign and no spaces") 49 | |> validate_length(:email, max: 160) 50 | |> unsafe_validate_unique(:email, SqliteScale.Repo) 51 | |> unique_constraint(:email) 52 | end 53 | 54 | defp validate_password(changeset, opts) do 55 | changeset 56 | |> validate_required([:password]) 57 | |> validate_length(:password, min: 12, max: 72) 58 | # |> validate_format(:password, ~r/[a-z]/, message: "at least one lower case character") 59 | # |> validate_format(:password, ~r/[A-Z]/, message: "at least one upper case character") 60 | # |> validate_format(:password, ~r/[!?@#$%^&*_0-9]/, message: "at least one digit or punctuation character") 61 | |> maybe_hash_password(opts) 62 | end 63 | 64 | defp maybe_hash_password(changeset, opts) do 65 | hash_password? = Keyword.get(opts, :hash_password, true) 66 | password = get_change(changeset, :password) 67 | 68 | if hash_password? && password && changeset.valid? do 69 | changeset 70 | # If using Bcrypt, then further validate it is at most 72 bytes long 71 | |> validate_length(:password, max: 72, count: :bytes) 72 | |> put_change(:hashed_password, Bcrypt.hash_pwd_salt(password)) 73 | |> delete_change(:password) 74 | else 75 | changeset 76 | end 77 | end 78 | 79 | @doc """ 80 | A user changeset for changing the email. 81 | 82 | It requires the email to change otherwise an error is added. 83 | """ 84 | def email_changeset(user, attrs) do 85 | user 86 | |> cast(attrs, [:email]) 87 | |> validate_email() 88 | |> case do 89 | %{changes: %{email: _}} = changeset -> changeset 90 | %{} = changeset -> add_error(changeset, :email, "did not change") 91 | end 92 | end 93 | 94 | @doc """ 95 | A user changeset for changing the password. 96 | 97 | ## Options 98 | 99 | * `:hash_password` - Hashes the password so it can be stored securely 100 | in the database and ensures the password field is cleared to prevent 101 | leaks in the logs. If password hashing is not needed and clearing the 102 | password field is not desired (like when using this changeset for 103 | validations on a LiveView form), this option can be set to `false`. 104 | Defaults to `true`. 105 | """ 106 | def password_changeset(user, attrs, opts \\ []) do 107 | user 108 | |> cast(attrs, [:password]) 109 | |> validate_confirmation(:password, message: "does not match password") 110 | |> validate_password(opts) 111 | end 112 | 113 | @doc """ 114 | Confirms the account by setting `confirmed_at`. 115 | """ 116 | def confirm_changeset(user) do 117 | now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second) 118 | change(user, confirmed_at: now) 119 | end 120 | 121 | @doc """ 122 | Verifies the password. 123 | 124 | If there is no user or the user doesn't have a password, we call 125 | `Bcrypt.no_user_verify/0` to avoid timing attacks. 126 | """ 127 | def valid_password?(%SqliteScale.Accounts.User{hashed_password: hashed_password}, password) 128 | when is_binary(hashed_password) and byte_size(password) > 0 do 129 | Bcrypt.verify_pass(password, hashed_password) 130 | end 131 | 132 | def valid_password?(_, _) do 133 | Bcrypt.no_user_verify() 134 | false 135 | end 136 | 137 | @doc """ 138 | Validates the current password otherwise adds an error to the changeset. 139 | """ 140 | def validate_current_password(changeset, password) do 141 | if valid_password?(changeset.data, password) do 142 | changeset 143 | else 144 | add_error(changeset, :current_password, "is not valid") 145 | end 146 | end 147 | end 148 | -------------------------------------------------------------------------------- /lib/sqlite_scale/accounts/user_notifier.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.Accounts.UserNotifier do 2 | import Swoosh.Email 3 | 4 | alias SqliteScale.Mailer 5 | 6 | # Delivers the email using the application mailer. 7 | defp deliver(recipient, subject, body) do 8 | email = 9 | new() 10 | |> to(recipient) 11 | |> from({"MyApp", "contact@example.com"}) 12 | |> subject(subject) 13 | |> text_body(body) 14 | 15 | with {:ok, _metadata} <- Mailer.deliver(email) do 16 | {:ok, email} 17 | end 18 | end 19 | 20 | @doc """ 21 | Deliver instructions to confirm account. 22 | """ 23 | def deliver_confirmation_instructions(user, url) do 24 | deliver(user.email, "Confirmation instructions", """ 25 | 26 | ============================== 27 | 28 | Hi #{user.email}, 29 | 30 | You can confirm your account by visiting the URL below: 31 | 32 | #{url} 33 | 34 | If you didn't create an account with us, please ignore this. 35 | 36 | ============================== 37 | """) 38 | end 39 | 40 | @doc """ 41 | Deliver instructions to reset a user password. 42 | """ 43 | def deliver_reset_password_instructions(user, url) do 44 | deliver(user.email, "Reset password instructions", """ 45 | 46 | ============================== 47 | 48 | Hi #{user.email}, 49 | 50 | You can reset your password by visiting the URL below: 51 | 52 | #{url} 53 | 54 | If you didn't request this change, please ignore this. 55 | 56 | ============================== 57 | """) 58 | end 59 | 60 | @doc """ 61 | Deliver instructions to update a user email. 62 | """ 63 | def deliver_update_email_instructions(user, url) do 64 | deliver(user.email, "Update email instructions", """ 65 | 66 | ============================== 67 | 68 | Hi #{user.email}, 69 | 70 | You can change your email by visiting the URL below: 71 | 72 | #{url} 73 | 74 | If you didn't request this change, please ignore this. 75 | 76 | ============================== 77 | """) 78 | end 79 | end 80 | -------------------------------------------------------------------------------- /lib/sqlite_scale/accounts/user_token.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.Accounts.UserToken do 2 | use Ecto.Schema 3 | import Ecto.Query 4 | 5 | @hash_algorithm :sha256 6 | @rand_size 32 7 | 8 | # It is very important to keep the reset password token expiry short, 9 | # since someone with access to the email may take over the account. 10 | @reset_password_validity_in_days 1 11 | @confirm_validity_in_days 7 12 | @change_email_validity_in_days 7 13 | @session_validity_in_days 60 14 | 15 | @primary_key {:id, :binary_id, autogenerate: true} 16 | @foreign_key_type :binary_id 17 | schema "users_tokens" do 18 | field :token, :binary 19 | field :context, :string 20 | field :sent_to, :string 21 | belongs_to :user, SqliteScale.Accounts.User 22 | 23 | timestamps(updated_at: false) 24 | end 25 | 26 | @doc """ 27 | Generates a token that will be stored in a signed place, 28 | such as session or cookie. As they are signed, those 29 | tokens do not need to be hashed. 30 | 31 | The reason why we store session tokens in the database, even 32 | though Phoenix already provides a session cookie, is because 33 | Phoenix' default session cookies are not persisted, they are 34 | simply signed and potentially encrypted. This means they are 35 | valid indefinitely, unless you change the signing/encryption 36 | salt. 37 | 38 | Therefore, storing them allows individual user 39 | sessions to be expired. The token system can also be extended 40 | to store additional data, such as the device used for logging in. 41 | You could then use this information to display all valid sessions 42 | and devices in the UI and allow users to explicitly expire any 43 | session they deem invalid. 44 | """ 45 | def build_session_token(user) do 46 | token = :crypto.strong_rand_bytes(@rand_size) 47 | {token, %SqliteScale.Accounts.UserToken{token: token, context: "session", user_id: user.id}} 48 | end 49 | 50 | @doc """ 51 | Checks if the token is valid and returns its underlying lookup query. 52 | 53 | The query returns the user found by the token, if any. 54 | 55 | The token is valid if it matches the value in the database and it has 56 | not expired (after @session_validity_in_days). 57 | """ 58 | def verify_session_token_query(token) do 59 | query = 60 | from token in token_and_context_query(token, "session"), 61 | join: user in assoc(token, :user), 62 | where: token.inserted_at > ago(@session_validity_in_days, "day"), 63 | select: user 64 | 65 | {:ok, query} 66 | end 67 | 68 | @doc """ 69 | Builds a token and its hash to be delivered to the user's email. 70 | 71 | The non-hashed token is sent to the user email while the 72 | hashed part is stored in the database. The original token cannot be reconstructed, 73 | which means anyone with read-only access to the database cannot directly use 74 | the token in the application to gain access. Furthermore, if the user changes 75 | their email in the system, the tokens sent to the previous email are no longer 76 | valid. 77 | 78 | Users can easily adapt the existing code to provide other types of delivery methods, 79 | for example, by phone numbers. 80 | """ 81 | def build_email_token(user, context) do 82 | build_hashed_token(user, context, user.email) 83 | end 84 | 85 | defp build_hashed_token(user, context, sent_to) do 86 | token = :crypto.strong_rand_bytes(@rand_size) 87 | hashed_token = :crypto.hash(@hash_algorithm, token) 88 | 89 | {Base.url_encode64(token, padding: false), 90 | %SqliteScale.Accounts.UserToken{ 91 | token: hashed_token, 92 | context: context, 93 | sent_to: sent_to, 94 | user_id: user.id 95 | }} 96 | end 97 | 98 | @doc """ 99 | Checks if the token is valid and returns its underlying lookup query. 100 | 101 | The query returns the user found by the token, if any. 102 | 103 | The given token is valid if it matches its hashed counterpart in the 104 | database and the user email has not changed. This function also checks 105 | if the token is being used within a certain period, depending on the 106 | context. The default contexts supported by this function are either 107 | "confirm", for account confirmation emails, and "reset_password", 108 | for resetting the password. For verifying requests to change the email, 109 | see `verify_change_email_token_query/2`. 110 | """ 111 | def verify_email_token_query(token, context) do 112 | case Base.url_decode64(token, padding: false) do 113 | {:ok, decoded_token} -> 114 | hashed_token = :crypto.hash(@hash_algorithm, decoded_token) 115 | days = days_for_context(context) 116 | 117 | query = 118 | from token in token_and_context_query(hashed_token, context), 119 | join: user in assoc(token, :user), 120 | where: token.inserted_at > ago(^days, "day") and token.sent_to == user.email, 121 | select: user 122 | 123 | {:ok, query} 124 | 125 | :error -> 126 | :error 127 | end 128 | end 129 | 130 | defp days_for_context("confirm"), do: @confirm_validity_in_days 131 | defp days_for_context("reset_password"), do: @reset_password_validity_in_days 132 | 133 | @doc """ 134 | Checks if the token is valid and returns its underlying lookup query. 135 | 136 | The query returns the user found by the token, if any. 137 | 138 | This is used to validate requests to change the user 139 | email. It is different from `verify_email_token_query/2` precisely because 140 | `verify_email_token_query/2` validates the email has not changed, which is 141 | the starting point by this function. 142 | 143 | The given token is valid if it matches its hashed counterpart in the 144 | database and if it has not expired (after @change_email_validity_in_days). 145 | The context must always start with "change:". 146 | """ 147 | def verify_change_email_token_query(token, "change:" <> _ = context) do 148 | case Base.url_decode64(token, padding: false) do 149 | {:ok, decoded_token} -> 150 | hashed_token = :crypto.hash(@hash_algorithm, decoded_token) 151 | 152 | query = 153 | from token in token_and_context_query(hashed_token, context), 154 | where: token.inserted_at > ago(@change_email_validity_in_days, "day") 155 | 156 | {:ok, query} 157 | 158 | :error -> 159 | :error 160 | end 161 | end 162 | 163 | @doc """ 164 | Returns the token struct for the given token value and context. 165 | """ 166 | def token_and_context_query(token, context) do 167 | from SqliteScale.Accounts.UserToken, where: [token: ^token, context: ^context] 168 | end 169 | 170 | @doc """ 171 | Gets all tokens for the given user for the given contexts. 172 | """ 173 | def user_and_contexts_query(user, :all) do 174 | from t in SqliteScale.Accounts.UserToken, where: t.user_id == ^user.id 175 | end 176 | 177 | def user_and_contexts_query(user, [_ | _] = contexts) do 178 | from t in SqliteScale.Accounts.UserToken, where: t.user_id == ^user.id and t.context in ^contexts 179 | end 180 | end 181 | -------------------------------------------------------------------------------- /lib/sqlite_scale/application.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.Application do 2 | # See https://hexdocs.pm/elixir/Application.html 3 | # for more information on OTP Applications 4 | @moduledoc false 5 | 6 | use Application 7 | 8 | @impl true 9 | def start(_type, _args) do 10 | children = [ 11 | # Start the Ecto repository 12 | SqliteScale.Repo, 13 | 14 | # Start the split SQLite supervision tree 15 | SqliteScale.DynamicRepoSupervisor, 16 | 17 | # Start the Telemetry supervisor 18 | SqliteScaleWeb.Telemetry, 19 | 20 | # Start the PubSub system 21 | {Phoenix.PubSub, name: SqliteScale.PubSub}, 22 | 23 | # Start the Endpoint (http/https) 24 | SqliteScaleWeb.Endpoint 25 | ] 26 | 27 | # See https://hexdocs.pm/elixir/Supervisor.html 28 | # for other strategies and supported options 29 | opts = [strategy: :one_for_one, name: SqliteScale.Supervisor] 30 | Supervisor.start_link(children, opts) 31 | end 32 | 33 | # Tell Phoenix to update the endpoint configuration 34 | # whenever the application is updated. 35 | @impl true 36 | def config_change(changed, _new, removed) do 37 | SqliteScaleWeb.Endpoint.config_change(changed, removed) 38 | :ok 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /lib/sqlite_scale/dynamic_repo_supervisor.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.DynamicRepoSupervisor do 2 | @moduledoc """ 3 | This supervision tree is used to start the registry 4 | and dynamic supervisor which handles each SQLite 5 | instance per user. 6 | """ 7 | 8 | use Supervisor 9 | 10 | alias SqliteScale.DynamicRepoSupervisor.RepoHydrator 11 | alias SqliteScale.DynamicRepoSupervisor.RepoRegistry 12 | alias SqliteScale.DynamicRepoSupervisor.RepoSupervisor 13 | 14 | def start_link(opts) do 15 | Supervisor.start_link(__MODULE__, opts, name: __MODULE__) 16 | end 17 | 18 | @impl true 19 | def init(opts) do 20 | children = [ 21 | RepoRegistry.child_spec(), 22 | RepoSupervisor, 23 | {RepoHydrator, opts} 24 | ] 25 | 26 | Supervisor.init(children, strategy: :one_for_all) 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /lib/sqlite_scale/dynamic_repo_supervisor/repo_hydrator.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.DynamicRepoSupervisor.RepoHydrator do 2 | @moduledoc """ 3 | This GenServer will start all of the repo processes dynamically 4 | based on what is in the priv dir. 5 | """ 6 | 7 | use GenServer 8 | 9 | alias SqliteScale.Accounts 10 | alias SqliteScale.Accounts.User 11 | alias SqliteScale.DynamicRepoSupervisor.RepoSupervisor 12 | 13 | def start_link(opts) do 14 | GenServer.start_link(__MODULE__, opts) 15 | end 16 | 17 | @impl true 18 | def init(opts) do 19 | Accounts.list_users() 20 | |> Enum.each(fn %User{} = user -> 21 | RepoSupervisor.add_repo_to_supervisor(user) 22 | end) 23 | 24 | :ignore 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /lib/sqlite_scale/dynamic_repo_supervisor/repo_registry.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.DynamicRepoSupervisor.RepoRegistry do 2 | @moduledoc """ 3 | This module is responsible for keeping track of all of the 4 | SQLite repos that are currently running. 5 | """ 6 | 7 | alias SqliteScale.Accounts.User 8 | 9 | @doc """ 10 | This function returns the child spec for this module so that it 11 | can easily be added to the supervision tree. 12 | """ 13 | def child_spec do 14 | Registry.child_spec( 15 | keys: :unique, 16 | name: __MODULE__, 17 | partitions: System.schedulers_online() 18 | ) 19 | end 20 | 21 | @doc """ 22 | This function looks up a repo process by its ID so that the 23 | processes can be then interacted with via its PID. 24 | """ 25 | def lookup_repo(%User{id: user_id}) do 26 | case Registry.lookup(__MODULE__, Ecto.UUID.cast!(user_id)) do 27 | [{repo_pid, _}] -> 28 | {:ok, repo_pid} 29 | 30 | [] -> 31 | {:error, :not_found} 32 | end 33 | end 34 | 35 | # The below functions are used under the hood when leveraging :via 36 | # to process PID lookup through a registry. 37 | 38 | @doc false 39 | def whereis_name(user_id) do 40 | case lookup_repo(user_id) do 41 | {:ok, repo_id} -> repo_id 42 | _ -> :undefined 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /lib/sqlite_scale/dynamic_repo_supervisor/repo_supervisor.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.DynamicRepoSupervisor.RepoSupervisor do 2 | @moduledoc """ 3 | This module is responsible for starting the repos as 4 | they are needed. 5 | """ 6 | 7 | use DynamicSupervisor 8 | 9 | require Logger 10 | 11 | alias SqliteScale.Accounts.User 12 | alias SqliteScale.DynamicRepoSupervisor.RepoRegistry 13 | alias SqliteScale.UserRepo 14 | 15 | @doc """ 16 | This function is used to start the DynamicSupervisor in the supervision tree 17 | """ 18 | def start_link(opts) do 19 | DynamicSupervisor.start_link(__MODULE__, opts, name: __MODULE__) 20 | end 21 | 22 | @impl true 23 | def init(_opts) do 24 | DynamicSupervisor.init(strategy: :one_for_one) 25 | end 26 | 27 | @doc """ 28 | Start a new repo process and adds it to the DynamicSupervisor. 29 | """ 30 | def add_repo_to_supervisor(%User{} = user) do 31 | user_id = Ecto.UUID.cast!(user.id) 32 | 33 | database_file = 34 | :sqlite_scale 35 | |> :code.priv_dir() 36 | |> Path.join("/user_repo/db_files/#{user_id}/") 37 | |> Path.join("user_data.db") 38 | 39 | repo_opts = [ 40 | name: {:via, Registry, {RepoRegistry, user_id}}, 41 | database: database_file, 42 | pool_size: 5, 43 | show_sensitive_data_on_connection_error: true 44 | ] 45 | 46 | child_spec = %{ 47 | id: UserRepo, 48 | start: {UserRepo, :start_link, [repo_opts]}, 49 | restart: :permanent 50 | } 51 | 52 | {:ok, pid} = DynamicSupervisor.start_child(__MODULE__, child_spec) 53 | run_migrations(user, pid) 54 | 55 | pid 56 | end 57 | 58 | @doc """ 59 | Gets all of the PIDs upnder this DynamicSupervisor. 60 | """ 61 | def all_repo_pids do 62 | __MODULE__ 63 | |> DynamicSupervisor.which_children() 64 | |> Enum.reduce([], fn {_, repo_pid, _, _}, acc -> 65 | [repo_pid | acc] 66 | end) 67 | end 68 | 69 | defp run_migrations(user, repo_pid) do 70 | # Run any pending migrations 71 | user 72 | |> UserRepo.with_dynamic_repo(fn -> 73 | Ecto.Migrator.run(UserRepo, :up, all: true, dynamic_repo: repo_pid) 74 | end) 75 | |> case do 76 | [] -> 77 | Logger.info("The database did not have any pending migrations") 78 | 79 | migrations when is_list(migrations) -> 80 | Logger.info("The database for UserRepo has applied the following migrations: #{Enum.join(migrations, ", ")}") 81 | end 82 | end 83 | end 84 | -------------------------------------------------------------------------------- /lib/sqlite_scale/mailer.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.Mailer do 2 | use Swoosh.Mailer, otp_app: :sqlite_scale 3 | end 4 | -------------------------------------------------------------------------------- /lib/sqlite_scale/repo.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.Repo do 2 | use Ecto.Repo, 3 | otp_app: :sqlite_scale, 4 | adapter: Ecto.Adapters.SQLite3 5 | end 6 | -------------------------------------------------------------------------------- /lib/sqlite_scale/todo_items.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.TodoItems do 2 | @moduledoc """ 3 | The TodoItems context. 4 | """ 5 | 6 | import Ecto.Query, warn: false 7 | alias SqliteScale.UserRepo 8 | 9 | alias SqliteScale.TodoItems.TodoItem 10 | 11 | @doc """ 12 | Returns the list of todo_items. 13 | 14 | ## Examples 15 | 16 | iex> list_todo_items() 17 | [%TodoItem{}, ...] 18 | 19 | """ 20 | def list_todo_items(user) do 21 | UserRepo.with_dynamic_repo(user, fn -> 22 | UserRepo.all(TodoItem) 23 | end) 24 | end 25 | 26 | @doc """ 27 | Gets a single todo_item. 28 | 29 | Raises `Ecto.NoResultsError` if the Todo item does not exist. 30 | 31 | ## Examples 32 | 33 | iex> get_todo_item!(123) 34 | %TodoItem{} 35 | 36 | iex> get_todo_item!(456) 37 | ** (Ecto.NoResultsError) 38 | 39 | """ 40 | def get_todo_item!(user, id) do 41 | UserRepo.with_dynamic_repo(user, fn -> 42 | UserRepo.get!(TodoItem, id) 43 | end) 44 | end 45 | 46 | @doc """ 47 | Creates a todo_item. 48 | 49 | ## Examples 50 | 51 | iex> create_todo_item(%{field: value}) 52 | {:ok, %TodoItem{}} 53 | 54 | iex> create_todo_item(%{field: bad_value}) 55 | {:error, %Ecto.Changeset{}} 56 | 57 | """ 58 | def create_todo_item(user, attrs \\ %{}) do 59 | UserRepo.with_dynamic_repo(user, fn -> 60 | %TodoItem{} 61 | |> TodoItem.changeset(attrs) 62 | |> UserRepo.insert() 63 | end) 64 | end 65 | 66 | @doc """ 67 | Updates a todo_item. 68 | 69 | ## Examples 70 | 71 | iex> update_todo_item(todo_item, %{field: new_value}) 72 | {:ok, %TodoItem{}} 73 | 74 | iex> update_todo_item(todo_item, %{field: bad_value}) 75 | {:error, %Ecto.Changeset{}} 76 | 77 | """ 78 | def update_todo_item(user, %TodoItem{} = todo_item, attrs) do 79 | UserRepo.with_dynamic_repo(user, fn -> 80 | todo_item 81 | |> TodoItem.changeset(attrs) 82 | |> UserRepo.update() 83 | end) 84 | end 85 | 86 | @doc """ 87 | Deletes a todo_item. 88 | 89 | ## Examples 90 | 91 | iex> delete_todo_item(todo_item) 92 | {:ok, %TodoItem{}} 93 | 94 | iex> delete_todo_item(todo_item) 95 | {:error, %Ecto.Changeset{}} 96 | 97 | """ 98 | def delete_todo_item(user, %TodoItem{} = todo_item) do 99 | UserRepo.with_dynamic_repo(user, fn -> 100 | UserRepo.delete(todo_item) 101 | end) 102 | end 103 | 104 | @doc """ 105 | Returns an `%Ecto.Changeset{}` for tracking todo_item changes. 106 | 107 | ## Examples 108 | 109 | iex> change_todo_item(todo_item) 110 | %Ecto.Changeset{data: %TodoItem{}} 111 | 112 | """ 113 | def change_todo_item(%TodoItem{} = todo_item, attrs \\ %{}) do 114 | TodoItem.changeset(todo_item, attrs) 115 | end 116 | end 117 | -------------------------------------------------------------------------------- /lib/sqlite_scale/todo_items/todo_item.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.TodoItems.TodoItem do 2 | @moduledoc """ 3 | The todo item ecto schema 4 | """ 5 | 6 | use Ecto.Schema 7 | 8 | import Ecto.Changeset 9 | 10 | @primary_key {:id, :binary_id, autogenerate: true} 11 | @foreign_key_type :binary_id 12 | schema "todo_items" do 13 | field :item, :string 14 | 15 | timestamps() 16 | end 17 | 18 | @doc false 19 | def changeset(todo_item, attrs) do 20 | todo_item 21 | |> cast(attrs, [:item]) 22 | |> validate_required([:item]) 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /lib/sqlite_scale/user_repo.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.UserRepo do 2 | use Ecto.Repo, 3 | otp_app: :sqlite_scale, 4 | adapter: Ecto.Adapters.SQLite3, 5 | database: nil 6 | 7 | require Logger 8 | 9 | alias SqliteScale.Accounts.User 10 | alias SqliteScale.DynamicRepoSupervisor.RepoRegistry 11 | 12 | def with_dynamic_repo(%User{} = user, callback) do 13 | with {:ok, repo} <- RepoRegistry.lookup_repo(user) do 14 | try do 15 | __MODULE__.put_dynamic_repo(repo) 16 | callback.() 17 | after 18 | __MODULE__.put_dynamic_repo(nil) 19 | end 20 | else 21 | error -> 22 | Logger.warning("Failed to get UserRepo for user: #{inspect(user)}") 23 | end 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb do 2 | @moduledoc """ 3 | The entrypoint for defining your web interface, such 4 | as controllers, views, channels and so on. 5 | 6 | This can be used in your application as: 7 | 8 | use SqliteScaleWeb, :controller 9 | use SqliteScaleWeb, :view 10 | 11 | The definitions below will be executed for every view, 12 | controller, etc, so keep them short and clean, focused 13 | on imports, uses and aliases. 14 | 15 | Do NOT define functions inside the quoted expressions 16 | below. Instead, define any helper function in modules 17 | and import those modules here. 18 | """ 19 | 20 | def controller do 21 | quote do 22 | use Phoenix.Controller, namespace: SqliteScaleWeb 23 | 24 | import Plug.Conn 25 | import SqliteScaleWeb.Gettext 26 | alias SqliteScaleWeb.Router.Helpers, as: Routes 27 | end 28 | end 29 | 30 | def view do 31 | quote do 32 | use Phoenix.View, 33 | root: "lib/sqlite_scale_web/templates", 34 | namespace: SqliteScaleWeb 35 | 36 | # Import convenience functions from controllers 37 | import Phoenix.Controller, 38 | only: [get_flash: 1, get_flash: 2, view_module: 1, view_template: 1] 39 | 40 | # Include shared imports and aliases for views 41 | unquote(view_helpers()) 42 | end 43 | end 44 | 45 | def live_view do 46 | quote do 47 | use Phoenix.LiveView, 48 | layout: {SqliteScaleWeb.LayoutView, "live.html"} 49 | 50 | unquote(view_helpers()) 51 | end 52 | end 53 | 54 | def live_component do 55 | quote do 56 | use Phoenix.LiveComponent 57 | 58 | unquote(view_helpers()) 59 | end 60 | end 61 | 62 | def component do 63 | quote do 64 | use Phoenix.Component 65 | 66 | unquote(view_helpers()) 67 | end 68 | end 69 | 70 | def router do 71 | quote do 72 | use Phoenix.Router 73 | 74 | import Plug.Conn 75 | import Phoenix.Controller 76 | import Phoenix.LiveView.Router 77 | end 78 | end 79 | 80 | def channel do 81 | quote do 82 | use Phoenix.Channel 83 | import SqliteScaleWeb.Gettext 84 | end 85 | end 86 | 87 | defp view_helpers do 88 | quote do 89 | # Use all HTML functionality (forms, tags, etc) 90 | use Phoenix.HTML 91 | 92 | # Import LiveView and .heex helpers (live_render, live_patch, <.form>, etc) 93 | import Phoenix.LiveView.Helpers 94 | 95 | # Import basic rendering functionality (render, render_layout, etc) 96 | import Phoenix.View 97 | 98 | import SqliteScaleWeb.ErrorHelpers 99 | import SqliteScaleWeb.Gettext 100 | alias SqliteScaleWeb.Router.Helpers, as: Routes 101 | end 102 | end 103 | 104 | @doc """ 105 | When used, dispatch to the appropriate controller/view/etc. 106 | """ 107 | defmacro __using__(which) when is_atom(which) do 108 | apply(__MODULE__, which, []) 109 | end 110 | end 111 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/controllers/page_controller.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.PageController do 2 | use SqliteScaleWeb, :controller 3 | 4 | def index(conn, _params) do 5 | render(conn, "index.html") 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/controllers/todo_item_controller.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.TodoItemController do 2 | use SqliteScaleWeb, :controller 3 | 4 | alias SqliteScale.TodoItems 5 | alias SqliteScale.TodoItems.TodoItem 6 | 7 | def index(conn, _params) do 8 | todo_items = TodoItems.list_todo_items(conn.assigns.current_user) 9 | render(conn, "index.html", todo_items: todo_items) 10 | end 11 | 12 | def new(conn, _params) do 13 | changeset = TodoItems.change_todo_item(%TodoItem{}) 14 | render(conn, "new.html", changeset: changeset) 15 | end 16 | 17 | def create(conn, %{"todo_item" => todo_item_params}) do 18 | case TodoItems.create_todo_item(conn.assigns.current_user, todo_item_params) do 19 | {:ok, todo_item} -> 20 | conn 21 | |> put_flash(:info, "Todo item created successfully.") 22 | |> redirect(to: Routes.todo_item_path(conn, :show, todo_item)) 23 | 24 | {:error, %Ecto.Changeset{} = changeset} -> 25 | render(conn, "new.html", changeset: changeset) 26 | end 27 | end 28 | 29 | def show(conn, %{"id" => id}) do 30 | todo_item = TodoItems.get_todo_item!(conn.assigns.current_user, id) 31 | render(conn, "show.html", todo_item: todo_item) 32 | end 33 | 34 | def edit(conn, %{"id" => id}) do 35 | todo_item = TodoItems.get_todo_item!(conn.assigns.current_user, id) 36 | changeset = TodoItems.change_todo_item(todo_item) 37 | render(conn, "edit.html", todo_item: todo_item, changeset: changeset) 38 | end 39 | 40 | def update(conn, %{"id" => id, "todo_item" => todo_item_params}) do 41 | todo_item = TodoItems.get_todo_item!(conn.assigns.current_user, id) 42 | 43 | case TodoItems.update_todo_item(conn.assigns.current_user, todo_item, todo_item_params) do 44 | {:ok, todo_item} -> 45 | conn 46 | |> put_flash(:info, "Todo item updated successfully.") 47 | |> redirect(to: Routes.todo_item_path(conn, :show, todo_item)) 48 | 49 | {:error, %Ecto.Changeset{} = changeset} -> 50 | render(conn, "edit.html", todo_item: todo_item, changeset: changeset) 51 | end 52 | end 53 | 54 | def delete(conn, %{"id" => id}) do 55 | todo_item = TodoItems.get_todo_item!(conn.assigns.current_user, id) 56 | {:ok, _todo_item} = TodoItems.delete_todo_item(conn.assigns.current_user, todo_item) 57 | 58 | conn 59 | |> put_flash(:info, "Todo item deleted successfully.") 60 | |> redirect(to: Routes.todo_item_path(conn, :index)) 61 | end 62 | end 63 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/controllers/user_auth.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.UserAuth do 2 | import Plug.Conn 3 | import Phoenix.Controller 4 | 5 | alias SqliteScale.Accounts 6 | alias SqliteScaleWeb.Router.Helpers, as: Routes 7 | 8 | # Make the remember me cookie valid for 60 days. 9 | # If you want bump or reduce this value, also change 10 | # the token expiry itself in UserToken. 11 | @max_age 60 * 60 * 24 * 60 12 | @remember_me_cookie "_sqlite_scale_web_user_remember_me" 13 | @remember_me_options [sign: true, max_age: @max_age, same_site: "Lax"] 14 | 15 | @doc """ 16 | Logs the user in. 17 | 18 | It renews the session ID and clears the whole session 19 | to avoid fixation attacks. See the renew_session 20 | function to customize this behaviour. 21 | 22 | It also sets a `:live_socket_id` key in the session, 23 | so LiveView sessions are identified and automatically 24 | disconnected on log out. The line can be safely removed 25 | if you are not using LiveView. 26 | """ 27 | def log_in_user(conn, user, params \\ %{}) do 28 | token = Accounts.generate_user_session_token(user) 29 | user_return_to = get_session(conn, :user_return_to) 30 | 31 | conn 32 | |> renew_session() 33 | |> put_session(:user_token, token) 34 | |> put_session(:live_socket_id, "users_sessions:#{Base.url_encode64(token)}") 35 | |> maybe_write_remember_me_cookie(token, params) 36 | |> redirect(to: user_return_to || signed_in_path(conn)) 37 | end 38 | 39 | defp maybe_write_remember_me_cookie(conn, token, %{"remember_me" => "true"}) do 40 | put_resp_cookie(conn, @remember_me_cookie, token, @remember_me_options) 41 | end 42 | 43 | defp maybe_write_remember_me_cookie(conn, _token, _params) do 44 | conn 45 | end 46 | 47 | # This function renews the session ID and erases the whole 48 | # session to avoid fixation attacks. If there is any data 49 | # in the session you may want to preserve after log in/log out, 50 | # you must explicitly fetch the session data before clearing 51 | # and then immediately set it after clearing, for example: 52 | # 53 | # defp renew_session(conn) do 54 | # preferred_locale = get_session(conn, :preferred_locale) 55 | # 56 | # conn 57 | # |> configure_session(renew: true) 58 | # |> clear_session() 59 | # |> put_session(:preferred_locale, preferred_locale) 60 | # end 61 | # 62 | defp renew_session(conn) do 63 | conn 64 | |> configure_session(renew: true) 65 | |> clear_session() 66 | end 67 | 68 | @doc """ 69 | Logs the user out. 70 | 71 | It clears all session data for safety. See renew_session. 72 | """ 73 | def log_out_user(conn) do 74 | user_token = get_session(conn, :user_token) 75 | user_token && Accounts.delete_session_token(user_token) 76 | 77 | if live_socket_id = get_session(conn, :live_socket_id) do 78 | SqliteScaleWeb.Endpoint.broadcast(live_socket_id, "disconnect", %{}) 79 | end 80 | 81 | conn 82 | |> renew_session() 83 | |> delete_resp_cookie(@remember_me_cookie) 84 | |> redirect(to: "/") 85 | end 86 | 87 | @doc """ 88 | Authenticates the user by looking into the session 89 | and remember me token. 90 | """ 91 | def fetch_current_user(conn, _opts) do 92 | {user_token, conn} = ensure_user_token(conn) 93 | user = user_token && Accounts.get_user_by_session_token(user_token) 94 | assign(conn, :current_user, user) 95 | end 96 | 97 | defp ensure_user_token(conn) do 98 | if user_token = get_session(conn, :user_token) do 99 | {user_token, conn} 100 | else 101 | conn = fetch_cookies(conn, signed: [@remember_me_cookie]) 102 | 103 | if user_token = conn.cookies[@remember_me_cookie] do 104 | {user_token, put_session(conn, :user_token, user_token)} 105 | else 106 | {nil, conn} 107 | end 108 | end 109 | end 110 | 111 | @doc """ 112 | Used for routes that require the user to not be authenticated. 113 | """ 114 | def redirect_if_user_is_authenticated(conn, _opts) do 115 | if conn.assigns[:current_user] do 116 | conn 117 | |> redirect(to: signed_in_path(conn)) 118 | |> halt() 119 | else 120 | conn 121 | end 122 | end 123 | 124 | @doc """ 125 | Used for routes that require the user to be authenticated. 126 | 127 | If you want to enforce the user email is confirmed before 128 | they use the application at all, here would be a good place. 129 | """ 130 | def require_authenticated_user(conn, _opts) do 131 | if conn.assigns[:current_user] do 132 | conn 133 | else 134 | conn 135 | |> put_flash(:error, "You must log in to access this page.") 136 | |> maybe_store_return_to() 137 | |> redirect(to: Routes.user_session_path(conn, :new)) 138 | |> halt() 139 | end 140 | end 141 | 142 | defp maybe_store_return_to(%{method: "GET"} = conn) do 143 | put_session(conn, :user_return_to, current_path(conn)) 144 | end 145 | 146 | defp maybe_store_return_to(conn), do: conn 147 | 148 | defp signed_in_path(_conn), do: "/" 149 | end 150 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/controllers/user_confirmation_controller.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.UserConfirmationController do 2 | use SqliteScaleWeb, :controller 3 | 4 | alias SqliteScale.Accounts 5 | 6 | def new(conn, _params) do 7 | render(conn, "new.html") 8 | end 9 | 10 | def create(conn, %{"user" => %{"email" => email}}) do 11 | if user = Accounts.get_user_by_email(email) do 12 | Accounts.deliver_user_confirmation_instructions( 13 | user, 14 | &Routes.user_confirmation_url(conn, :edit, &1) 15 | ) 16 | end 17 | 18 | conn 19 | |> put_flash( 20 | :info, 21 | "If your email is in our system and it has not been confirmed yet, " <> 22 | "you will receive an email with instructions shortly." 23 | ) 24 | |> redirect(to: "/") 25 | end 26 | 27 | def edit(conn, %{"token" => token}) do 28 | render(conn, "edit.html", token: token) 29 | end 30 | 31 | # Do not log in the user after confirmation to avoid a 32 | # leaked token giving the user access to the account. 33 | def update(conn, %{"token" => token}) do 34 | case Accounts.confirm_user(token) do 35 | {:ok, _} -> 36 | conn 37 | |> put_flash(:info, "User confirmed successfully.") 38 | |> redirect(to: "/") 39 | 40 | :error -> 41 | # If there is a current user and the account was already confirmed, 42 | # then odds are that the confirmation link was already visited, either 43 | # by some automation or by the user themselves, so we redirect without 44 | # a warning message. 45 | case conn.assigns do 46 | %{current_user: %{confirmed_at: confirmed_at}} when not is_nil(confirmed_at) -> 47 | redirect(conn, to: "/") 48 | 49 | %{} -> 50 | conn 51 | |> put_flash(:error, "User confirmation link is invalid or it has expired.") 52 | |> redirect(to: "/") 53 | end 54 | end 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/controllers/user_registration_controller.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.UserRegistrationController do 2 | use SqliteScaleWeb, :controller 3 | 4 | alias SqliteScale.Accounts 5 | alias SqliteScale.Accounts.User 6 | alias SqliteScaleWeb.UserAuth 7 | 8 | def new(conn, _params) do 9 | changeset = Accounts.change_user_registration(%User{}) 10 | render(conn, "new.html", changeset: changeset) 11 | end 12 | 13 | def create(conn, %{"user" => user_params}) do 14 | case Accounts.register_user(user_params) do 15 | {:ok, user} -> 16 | {:ok, _} = 17 | Accounts.deliver_user_confirmation_instructions( 18 | user, 19 | &Routes.user_confirmation_url(conn, :edit, &1) 20 | ) 21 | 22 | conn 23 | |> put_flash(:info, "User created successfully.") 24 | |> UserAuth.log_in_user(user) 25 | 26 | {:error, %Ecto.Changeset{} = changeset} -> 27 | render(conn, "new.html", changeset: changeset) 28 | end 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/controllers/user_reset_password_controller.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.UserResetPasswordController do 2 | use SqliteScaleWeb, :controller 3 | 4 | alias SqliteScale.Accounts 5 | 6 | plug :get_user_by_reset_password_token when action in [:edit, :update] 7 | 8 | def new(conn, _params) do 9 | render(conn, "new.html") 10 | end 11 | 12 | def create(conn, %{"user" => %{"email" => email}}) do 13 | if user = Accounts.get_user_by_email(email) do 14 | Accounts.deliver_user_reset_password_instructions( 15 | user, 16 | &Routes.user_reset_password_url(conn, :edit, &1) 17 | ) 18 | end 19 | 20 | conn 21 | |> put_flash( 22 | :info, 23 | "If your email is in our system, you will receive instructions to reset your password shortly." 24 | ) 25 | |> redirect(to: "/") 26 | end 27 | 28 | def edit(conn, _params) do 29 | render(conn, "edit.html", changeset: Accounts.change_user_password(conn.assigns.user)) 30 | end 31 | 32 | # Do not log in the user after reset password to avoid a 33 | # leaked token giving the user access to the account. 34 | def update(conn, %{"user" => user_params}) do 35 | case Accounts.reset_user_password(conn.assigns.user, user_params) do 36 | {:ok, _} -> 37 | conn 38 | |> put_flash(:info, "Password reset successfully.") 39 | |> redirect(to: Routes.user_session_path(conn, :new)) 40 | 41 | {:error, changeset} -> 42 | render(conn, "edit.html", changeset: changeset) 43 | end 44 | end 45 | 46 | defp get_user_by_reset_password_token(conn, _opts) do 47 | %{"token" => token} = conn.params 48 | 49 | if user = Accounts.get_user_by_reset_password_token(token) do 50 | conn |> assign(:user, user) |> assign(:token, token) 51 | else 52 | conn 53 | |> put_flash(:error, "Reset password link is invalid or it has expired.") 54 | |> redirect(to: "/") 55 | |> halt() 56 | end 57 | end 58 | end 59 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/controllers/user_session_controller.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.UserSessionController do 2 | use SqliteScaleWeb, :controller 3 | 4 | alias SqliteScale.Accounts 5 | alias SqliteScaleWeb.UserAuth 6 | 7 | def new(conn, _params) do 8 | render(conn, "new.html", error_message: nil) 9 | end 10 | 11 | def create(conn, %{"user" => user_params}) do 12 | %{"email" => email, "password" => password} = user_params 13 | 14 | if user = Accounts.get_user_by_email_and_password(email, password) do 15 | UserAuth.log_in_user(conn, user, user_params) 16 | else 17 | # In order to prevent user enumeration attacks, don't disclose whether the email is registered. 18 | render(conn, "new.html", error_message: "Invalid email or password") 19 | end 20 | end 21 | 22 | def delete(conn, _params) do 23 | conn 24 | |> put_flash(:info, "Logged out successfully.") 25 | |> UserAuth.log_out_user() 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/controllers/user_settings_controller.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.UserSettingsController do 2 | use SqliteScaleWeb, :controller 3 | 4 | alias SqliteScale.Accounts 5 | alias SqliteScaleWeb.UserAuth 6 | 7 | plug :assign_email_and_password_changesets 8 | 9 | def edit(conn, _params) do 10 | render(conn, "edit.html") 11 | end 12 | 13 | def update(conn, %{"action" => "update_email"} = params) do 14 | %{"current_password" => password, "user" => user_params} = params 15 | user = conn.assigns.current_user 16 | 17 | case Accounts.apply_user_email(user, password, user_params) do 18 | {:ok, applied_user} -> 19 | Accounts.deliver_update_email_instructions( 20 | applied_user, 21 | user.email, 22 | &Routes.user_settings_url(conn, :confirm_email, &1) 23 | ) 24 | 25 | conn 26 | |> put_flash( 27 | :info, 28 | "A link to confirm your email change has been sent to the new address." 29 | ) 30 | |> redirect(to: Routes.user_settings_path(conn, :edit)) 31 | 32 | {:error, changeset} -> 33 | render(conn, "edit.html", email_changeset: changeset) 34 | end 35 | end 36 | 37 | def update(conn, %{"action" => "update_password"} = params) do 38 | %{"current_password" => password, "user" => user_params} = params 39 | user = conn.assigns.current_user 40 | 41 | case Accounts.update_user_password(user, password, user_params) do 42 | {:ok, user} -> 43 | conn 44 | |> put_flash(:info, "Password updated successfully.") 45 | |> put_session(:user_return_to, Routes.user_settings_path(conn, :edit)) 46 | |> UserAuth.log_in_user(user) 47 | 48 | {:error, changeset} -> 49 | render(conn, "edit.html", password_changeset: changeset) 50 | end 51 | end 52 | 53 | def confirm_email(conn, %{"token" => token}) do 54 | case Accounts.update_user_email(conn.assigns.current_user, token) do 55 | :ok -> 56 | conn 57 | |> put_flash(:info, "Email changed successfully.") 58 | |> redirect(to: Routes.user_settings_path(conn, :edit)) 59 | 60 | :error -> 61 | conn 62 | |> put_flash(:error, "Email change link is invalid or it has expired.") 63 | |> redirect(to: Routes.user_settings_path(conn, :edit)) 64 | end 65 | end 66 | 67 | defp assign_email_and_password_changesets(conn, _opts) do 68 | user = conn.assigns.current_user 69 | 70 | conn 71 | |> assign(:email_changeset, Accounts.change_user_email(user)) 72 | |> assign(:password_changeset, Accounts.change_user_password(user)) 73 | end 74 | end 75 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/endpoint.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.Endpoint do 2 | use Phoenix.Endpoint, otp_app: :sqlite_scale 3 | 4 | # The session will be stored in the cookie and signed, 5 | # this means its contents can be read but not tampered with. 6 | # Set :encryption_salt if you would also like to encrypt it. 7 | @session_options [ 8 | store: :cookie, 9 | key: "_sqlite_scale_key", 10 | signing_salt: "IGw/Wshm" 11 | ] 12 | 13 | socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]] 14 | 15 | # Serve at "/" the static files from "priv/static" directory. 16 | # 17 | # You should set gzip to true if you are running phx.digest 18 | # when deploying your static files in production. 19 | plug Plug.Static, 20 | at: "/", 21 | from: :sqlite_scale, 22 | gzip: false, 23 | only: ~w(assets fonts images favicon.ico robots.txt) 24 | 25 | # Code reloading can be explicitly enabled under the 26 | # :code_reloader configuration of your endpoint. 27 | if code_reloading? do 28 | socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket 29 | plug Phoenix.LiveReloader 30 | plug Phoenix.CodeReloader 31 | plug Phoenix.Ecto.CheckRepoStatus, otp_app: :sqlite_scale 32 | end 33 | 34 | plug Phoenix.LiveDashboard.RequestLogger, 35 | param_key: "request_logger", 36 | cookie_key: "request_logger" 37 | 38 | plug Plug.RequestId 39 | plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint] 40 | 41 | plug Plug.Parsers, 42 | parsers: [:urlencoded, :multipart, :json], 43 | pass: ["*/*"], 44 | json_decoder: Phoenix.json_library() 45 | 46 | plug Plug.MethodOverride 47 | plug Plug.Head 48 | plug Plug.Session, @session_options 49 | plug SqliteScaleWeb.Router 50 | end 51 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/gettext.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.Gettext do 2 | @moduledoc """ 3 | A module providing Internationalization with a gettext-based API. 4 | 5 | By using [Gettext](https://hexdocs.pm/gettext), 6 | your module gains a set of macros for translations, for example: 7 | 8 | import SqliteScaleWeb.Gettext 9 | 10 | # Simple translation 11 | gettext("Here is the string to translate") 12 | 13 | # Plural translation 14 | ngettext("Here is the string to translate", 15 | "Here are the strings to translate", 16 | 3) 17 | 18 | # Domain-based translation 19 | dgettext("errors", "Here is the error message to translate") 20 | 21 | See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage. 22 | """ 23 | use Gettext, otp_app: :sqlite_scale 24 | end 25 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/router.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.Router do 2 | use SqliteScaleWeb, :router 3 | 4 | import SqliteScaleWeb.UserAuth 5 | 6 | pipeline :browser do 7 | plug :accepts, ["html"] 8 | plug :fetch_session 9 | plug :fetch_live_flash 10 | plug :put_root_layout, {SqliteScaleWeb.LayoutView, :root} 11 | plug :protect_from_forgery 12 | plug :put_secure_browser_headers 13 | plug :fetch_current_user 14 | end 15 | 16 | pipeline :api do 17 | plug :accepts, ["json"] 18 | end 19 | 20 | scope "/", SqliteScaleWeb do 21 | pipe_through :browser 22 | 23 | get "/", PageController, :index 24 | end 25 | 26 | # Other scopes may use custom stacks. 27 | # scope "/api", SqliteScaleWeb do 28 | # pipe_through :api 29 | # end 30 | 31 | # Enables LiveDashboard only for development 32 | # 33 | # If you want to use the LiveDashboard in production, you should put 34 | # it behind authentication and allow only admins to access it. 35 | # If your application does not have an admins-only section yet, 36 | # you can use Plug.BasicAuth to set up some basic authentication 37 | # as long as you are also using SSL (which you should anyway). 38 | if Mix.env() in [:dev, :test] do 39 | import Phoenix.LiveDashboard.Router 40 | 41 | scope "/" do 42 | pipe_through :browser 43 | 44 | live_dashboard "/dashboard", metrics: SqliteScaleWeb.Telemetry 45 | end 46 | end 47 | 48 | # Enables the Swoosh mailbox preview in development. 49 | # 50 | # Note that preview only shows emails that were sent by the same 51 | # node running the Phoenix server. 52 | if Mix.env() == :dev do 53 | scope "/dev" do 54 | pipe_through :browser 55 | 56 | forward "/mailbox", Plug.Swoosh.MailboxPreview 57 | end 58 | end 59 | 60 | ## Authentication routes 61 | 62 | scope "/", SqliteScaleWeb do 63 | pipe_through [:browser, :redirect_if_user_is_authenticated] 64 | 65 | get "/users/register", UserRegistrationController, :new 66 | post "/users/register", UserRegistrationController, :create 67 | get "/users/log_in", UserSessionController, :new 68 | post "/users/log_in", UserSessionController, :create 69 | get "/users/reset_password", UserResetPasswordController, :new 70 | post "/users/reset_password", UserResetPasswordController, :create 71 | get "/users/reset_password/:token", UserResetPasswordController, :edit 72 | put "/users/reset_password/:token", UserResetPasswordController, :update 73 | end 74 | 75 | scope "/", SqliteScaleWeb do 76 | pipe_through [:browser, :require_authenticated_user] 77 | 78 | get "/users/settings", UserSettingsController, :edit 79 | put "/users/settings", UserSettingsController, :update 80 | get "/users/settings/confirm_email/:token", UserSettingsController, :confirm_email 81 | 82 | resources "/todo_items", TodoItemController 83 | end 84 | 85 | scope "/", SqliteScaleWeb do 86 | pipe_through [:browser] 87 | 88 | delete "/users/log_out", UserSessionController, :delete 89 | get "/users/confirm", UserConfirmationController, :new 90 | post "/users/confirm", UserConfirmationController, :create 91 | get "/users/confirm/:token", UserConfirmationController, :edit 92 | post "/users/confirm/:token", UserConfirmationController, :update 93 | end 94 | end 95 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/telemetry.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.Telemetry do 2 | use Supervisor 3 | import Telemetry.Metrics 4 | 5 | def start_link(arg) do 6 | Supervisor.start_link(__MODULE__, arg, name: __MODULE__) 7 | end 8 | 9 | @impl true 10 | def init(_arg) do 11 | children = [ 12 | # Telemetry poller will execute the given period measurements 13 | # every 10_000ms. Learn more here: https://hexdocs.pm/telemetry_metrics 14 | {:telemetry_poller, measurements: periodic_measurements(), period: 10_000} 15 | # Add reporters as children of your supervision tree. 16 | # {Telemetry.Metrics.ConsoleReporter, metrics: metrics()} 17 | ] 18 | 19 | Supervisor.init(children, strategy: :one_for_one) 20 | end 21 | 22 | def metrics do 23 | [ 24 | # Phoenix Metrics 25 | summary("phoenix.endpoint.stop.duration", 26 | unit: {:native, :millisecond} 27 | ), 28 | summary("phoenix.router_dispatch.stop.duration", 29 | tags: [:route], 30 | unit: {:native, :millisecond} 31 | ), 32 | 33 | # Database Metrics 34 | summary("sqlite_scale.repo.query.total_time", 35 | unit: {:native, :millisecond}, 36 | description: "The sum of the other measurements" 37 | ), 38 | summary("sqlite_scale.repo.query.decode_time", 39 | unit: {:native, :millisecond}, 40 | description: "The time spent decoding the data received from the database" 41 | ), 42 | summary("sqlite_scale.repo.query.query_time", 43 | unit: {:native, :millisecond}, 44 | description: "The time spent executing the query" 45 | ), 46 | summary("sqlite_scale.repo.query.queue_time", 47 | unit: {:native, :millisecond}, 48 | description: "The time spent waiting for a database connection" 49 | ), 50 | summary("sqlite_scale.repo.query.idle_time", 51 | unit: {:native, :millisecond}, 52 | description: 53 | "The time the connection spent waiting before being checked out for the query" 54 | ), 55 | 56 | # VM Metrics 57 | summary("vm.memory.total", unit: {:byte, :kilobyte}), 58 | summary("vm.total_run_queue_lengths.total"), 59 | summary("vm.total_run_queue_lengths.cpu"), 60 | summary("vm.total_run_queue_lengths.io") 61 | ] 62 | end 63 | 64 | defp periodic_measurements do 65 | [ 66 | # A module, function and arguments to be invoked periodically. 67 | # This function must call :telemetry.execute/3 and a metric must be added above. 68 | # {SqliteScaleWeb, :count_users, []} 69 | ] 70 | end 71 | end 72 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/templates/layout/_user_menu.html.heex: -------------------------------------------------------------------------------- 1 | 11 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/templates/layout/app.html.heex: -------------------------------------------------------------------------------- 1 |
2 | 3 | 4 | <%= @inner_content %> 5 |
6 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/templates/layout/live.html.heex: -------------------------------------------------------------------------------- 1 |
2 | 5 | 6 | 9 | 10 | <%= @inner_content %> 11 |
12 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/templates/layout/root.html.heex: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | <%= csrf_meta_tag() %> 8 | <%= live_title_tag assigns[:page_title] || "SqliteScale", suffix: " · Phoenix Framework" %> 9 | 10 | 11 | 12 | 13 |
14 |
15 | 24 | 27 |
28 |
29 | <%= @inner_content %> 30 | 31 | 32 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/templates/page/index.html.heex: -------------------------------------------------------------------------------- 1 |
2 |

<%= gettext "Welcome to %{name}!", name: "Phoenix" %>

3 |

Peace of mind from prototype to production

4 |
5 | 6 |
7 |
8 |

Resources

9 | 20 |
21 |
22 |

Help

23 | 40 |
41 |
42 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/templates/todo_item/edit.html.heex: -------------------------------------------------------------------------------- 1 |

Edit Todo item

2 | 3 | <%= render "form.html", Map.put(assigns, :action, Routes.todo_item_path(@conn, :update, @todo_item)) %> 4 | 5 | <%= link "Back", to: Routes.todo_item_path(@conn, :index) %> 6 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/templates/todo_item/form.html.heex: -------------------------------------------------------------------------------- 1 | <.form let={f} for={@changeset} action={@action}> 2 | <%= if @changeset.action do %> 3 |
4 |

Oops, something went wrong! Please check the errors below.

5 |
6 | <% end %> 7 | 8 | <%= label f, :item %> 9 | <%= text_input f, :item %> 10 | <%= error_tag f, :item %> 11 | 12 |
13 | <%= submit "Save" %> 14 |
15 | 16 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/templates/todo_item/index.html.heex: -------------------------------------------------------------------------------- 1 |

Listing Todo items

2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | <%= for todo_item <- @todo_items do %> 13 | 14 | 15 | 16 | 21 | 22 | <% end %> 23 | 24 |
Item
<%= todo_item.item %> 17 | <%= link "Show", to: Routes.todo_item_path(@conn, :show, todo_item) %> 18 | <%= link "Edit", to: Routes.todo_item_path(@conn, :edit, todo_item) %> 19 | <%= link "Delete", to: Routes.todo_item_path(@conn, :delete, todo_item), method: :delete, data: [confirm: "Are you sure?"] %> 20 |
25 | 26 | <%= link "New Todo item", to: Routes.todo_item_path(@conn, :new) %> 27 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/templates/todo_item/new.html.heex: -------------------------------------------------------------------------------- 1 |

New Todo item

2 | 3 | <%= render "form.html", Map.put(assigns, :action, Routes.todo_item_path(@conn, :create)) %> 4 | 5 | <%= link "Back", to: Routes.todo_item_path(@conn, :index) %> 6 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/templates/todo_item/show.html.heex: -------------------------------------------------------------------------------- 1 |

Show Todo item

2 | 3 | 11 | 12 | <%= link "Edit", to: Routes.todo_item_path(@conn, :edit, @todo_item) %> | 13 | <%= link "Back", to: Routes.todo_item_path(@conn, :index) %> 14 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/templates/user_confirmation/edit.html.heex: -------------------------------------------------------------------------------- 1 |

Confirm account

2 | 3 | <.form let={_f} for={:user} action={Routes.user_confirmation_path(@conn, :update, @token)}> 4 |
5 | <%= submit "Confirm my account" %> 6 |
7 | 8 | 9 |

10 | <%= link "Register", to: Routes.user_registration_path(@conn, :new) %> | 11 | <%= link "Log in", to: Routes.user_session_path(@conn, :new) %> 12 |

13 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/templates/user_confirmation/new.html.heex: -------------------------------------------------------------------------------- 1 |

Resend confirmation instructions

2 | 3 | <.form let={f} for={:user} action={Routes.user_confirmation_path(@conn, :create)}> 4 | <%= label f, :email %> 5 | <%= email_input f, :email, required: true %> 6 | 7 |
8 | <%= submit "Resend confirmation instructions" %> 9 |
10 | 11 | 12 |

13 | <%= link "Register", to: Routes.user_registration_path(@conn, :new) %> | 14 | <%= link "Log in", to: Routes.user_session_path(@conn, :new) %> 15 |

16 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/templates/user_registration/new.html.heex: -------------------------------------------------------------------------------- 1 |

Register

2 | 3 | <.form let={f} for={@changeset} action={Routes.user_registration_path(@conn, :create)}> 4 | <%= if @changeset.action do %> 5 |
6 |

Oops, something went wrong! Please check the errors below.

7 |
8 | <% end %> 9 | 10 | <%= label f, :email %> 11 | <%= email_input f, :email, required: true %> 12 | <%= error_tag f, :email %> 13 | 14 | <%= label f, :password %> 15 | <%= password_input f, :password, required: true %> 16 | <%= error_tag f, :password %> 17 | 18 |
19 | <%= submit "Register" %> 20 |
21 | 22 | 23 |

24 | <%= link "Log in", to: Routes.user_session_path(@conn, :new) %> | 25 | <%= link "Forgot your password?", to: Routes.user_reset_password_path(@conn, :new) %> 26 |

27 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/templates/user_reset_password/edit.html.heex: -------------------------------------------------------------------------------- 1 |

Reset password

2 | 3 | <.form let={f} for={@changeset} action={Routes.user_reset_password_path(@conn, :update, @token)}> 4 | <%= if @changeset.action do %> 5 |
6 |

Oops, something went wrong! Please check the errors below.

7 |
8 | <% end %> 9 | 10 | <%= label f, :password, "New password" %> 11 | <%= password_input f, :password, required: true %> 12 | <%= error_tag f, :password %> 13 | 14 | <%= label f, :password_confirmation, "Confirm new password" %> 15 | <%= password_input f, :password_confirmation, required: true %> 16 | <%= error_tag f, :password_confirmation %> 17 | 18 |
19 | <%= submit "Reset password" %> 20 |
21 | 22 | 23 |

24 | <%= link "Register", to: Routes.user_registration_path(@conn, :new) %> | 25 | <%= link "Log in", to: Routes.user_session_path(@conn, :new) %> 26 |

27 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/templates/user_reset_password/new.html.heex: -------------------------------------------------------------------------------- 1 |

Forgot your password?

2 | 3 | <.form let={f} for={:user} action={Routes.user_reset_password_path(@conn, :create)}> 4 | <%= label f, :email %> 5 | <%= email_input f, :email, required: true %> 6 | 7 |
8 | <%= submit "Send instructions to reset password" %> 9 |
10 | 11 | 12 |

13 | <%= link "Register", to: Routes.user_registration_path(@conn, :new) %> | 14 | <%= link "Log in", to: Routes.user_session_path(@conn, :new) %> 15 |

16 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/templates/user_session/new.html.heex: -------------------------------------------------------------------------------- 1 |

Log in

2 | 3 | <.form let={f} for={@conn} action={Routes.user_session_path(@conn, :create)} as={:user}> 4 | <%= if @error_message do %> 5 |
6 |

<%= @error_message %>

7 |
8 | <% end %> 9 | 10 | <%= label f, :email %> 11 | <%= email_input f, :email, required: true %> 12 | 13 | <%= label f, :password %> 14 | <%= password_input f, :password, required: true %> 15 | 16 | <%= label f, :remember_me, "Keep me logged in for 60 days" %> 17 | <%= checkbox f, :remember_me %> 18 | 19 |
20 | <%= submit "Log in" %> 21 |
22 | 23 | 24 |

25 | <%= link "Register", to: Routes.user_registration_path(@conn, :new) %> | 26 | <%= link "Forgot your password?", to: Routes.user_reset_password_path(@conn, :new) %> 27 |

28 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/templates/user_settings/edit.html.heex: -------------------------------------------------------------------------------- 1 |

Settings

2 | 3 |

Change email

4 | 5 | <.form let={f} for={@email_changeset} action={Routes.user_settings_path(@conn, :update)} id="update_email"> 6 | <%= if @email_changeset.action do %> 7 |
8 |

Oops, something went wrong! Please check the errors below.

9 |
10 | <% end %> 11 | 12 | <%= hidden_input f, :action, name: "action", value: "update_email" %> 13 | 14 | <%= label f, :email %> 15 | <%= email_input f, :email, required: true %> 16 | <%= error_tag f, :email %> 17 | 18 | <%= label f, :current_password, for: "current_password_for_email" %> 19 | <%= password_input f, :current_password, required: true, name: "current_password", id: "current_password_for_email" %> 20 | <%= error_tag f, :current_password %> 21 | 22 |
23 | <%= submit "Change email" %> 24 |
25 | 26 | 27 |

Change password

28 | 29 | <.form let={f} for={@password_changeset} action={Routes.user_settings_path(@conn, :update)} id="update_password"> 30 | <%= if @password_changeset.action do %> 31 |
32 |

Oops, something went wrong! Please check the errors below.

33 |
34 | <% end %> 35 | 36 | <%= hidden_input f, :action, name: "action", value: "update_password" %> 37 | 38 | <%= label f, :password, "New password" %> 39 | <%= password_input f, :password, required: true %> 40 | <%= error_tag f, :password %> 41 | 42 | <%= label f, :password_confirmation, "Confirm new password" %> 43 | <%= password_input f, :password_confirmation, required: true %> 44 | <%= error_tag f, :password_confirmation %> 45 | 46 | <%= label f, :current_password, for: "current_password_for_password" %> 47 | <%= password_input f, :current_password, required: true, name: "current_password", id: "current_password_for_password" %> 48 | <%= error_tag f, :current_password %> 49 | 50 |
51 | <%= submit "Change password" %> 52 |
53 | 54 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/views/error_helpers.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.ErrorHelpers do 2 | @moduledoc """ 3 | Conveniences for translating and building error messages. 4 | """ 5 | 6 | use Phoenix.HTML 7 | 8 | @doc """ 9 | Generates tag for inlined form input errors. 10 | """ 11 | def error_tag(form, field) do 12 | Enum.map(Keyword.get_values(form.errors, field), fn error -> 13 | content_tag(:span, translate_error(error), 14 | class: "invalid-feedback", 15 | phx_feedback_for: input_name(form, field) 16 | ) 17 | end) 18 | end 19 | 20 | @doc """ 21 | Translates an error message using gettext. 22 | """ 23 | def translate_error({msg, opts}) do 24 | # When using gettext, we typically pass the strings we want 25 | # to translate as a static argument: 26 | # 27 | # # Translate "is invalid" in the "errors" domain 28 | # dgettext("errors", "is invalid") 29 | # 30 | # # Translate the number of files with plural rules 31 | # dngettext("errors", "1 file", "%{count} files", count) 32 | # 33 | # Because the error messages we show in our forms and APIs 34 | # are defined inside Ecto, we need to translate them dynamically. 35 | # This requires us to call the Gettext module passing our gettext 36 | # backend as first argument. 37 | # 38 | # Note we use the "errors" domain, which means translations 39 | # should be written to the errors.po file. The :count option is 40 | # set by Ecto and indicates we should also apply plural rules. 41 | if count = opts[:count] do 42 | Gettext.dngettext(SqliteScaleWeb.Gettext, "errors", msg, msg, count, opts) 43 | else 44 | Gettext.dgettext(SqliteScaleWeb.Gettext, "errors", msg, opts) 45 | end 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/views/error_view.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.ErrorView do 2 | use SqliteScaleWeb, :view 3 | 4 | # If you want to customize a particular status code 5 | # for a certain format, you may uncomment below. 6 | # def render("500.html", _assigns) do 7 | # "Internal Server Error" 8 | # end 9 | 10 | # By default, Phoenix returns the status message from 11 | # the template name. For example, "404.html" becomes 12 | # "Not Found". 13 | def template_not_found(template, _assigns) do 14 | Phoenix.Controller.status_message_from_template(template) 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/views/layout_view.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.LayoutView do 2 | use SqliteScaleWeb, :view 3 | 4 | # Phoenix LiveDashboard is available only in development by default, 5 | # so we instruct Elixir to not warn if the dashboard route is missing. 6 | @compile {:no_warn_undefined, {Routes, :live_dashboard_path, 2}} 7 | end 8 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/views/page_view.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.PageView do 2 | use SqliteScaleWeb, :view 3 | end 4 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/views/todo_item_view.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.TodoItemView do 2 | use SqliteScaleWeb, :view 3 | end 4 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/views/user_confirmation_view.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.UserConfirmationView do 2 | use SqliteScaleWeb, :view 3 | end 4 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/views/user_registration_view.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.UserRegistrationView do 2 | use SqliteScaleWeb, :view 3 | end 4 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/views/user_reset_password_view.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.UserResetPasswordView do 2 | use SqliteScaleWeb, :view 3 | end 4 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/views/user_session_view.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.UserSessionView do 2 | use SqliteScaleWeb, :view 3 | end 4 | -------------------------------------------------------------------------------- /lib/sqlite_scale_web/views/user_settings_view.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.UserSettingsView do 2 | use SqliteScaleWeb, :view 3 | end 4 | -------------------------------------------------------------------------------- /mix.exs: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.MixProject do 2 | use Mix.Project 3 | 4 | def project do 5 | [ 6 | app: :sqlite_scale, 7 | version: "0.1.0", 8 | elixir: "~> 1.12", 9 | elixirc_paths: elixirc_paths(Mix.env()), 10 | compilers: [:gettext] ++ Mix.compilers(), 11 | start_permanent: Mix.env() == :prod, 12 | aliases: aliases(), 13 | deps: deps() 14 | ] 15 | end 16 | 17 | # Configuration for the OTP application. 18 | # 19 | # Type `mix help compile.app` for more information. 20 | def application do 21 | [ 22 | mod: {SqliteScale.Application, []}, 23 | extra_applications: [:logger, :runtime_tools] 24 | ] 25 | end 26 | 27 | # Specifies which paths to compile per environment. 28 | defp elixirc_paths(:test), do: ["lib", "test/support"] 29 | defp elixirc_paths(_), do: ["lib"] 30 | 31 | # Specifies your project dependencies. 32 | # 33 | # Type `mix help deps` for examples and options. 34 | defp deps do 35 | [ 36 | {:bcrypt_elixir, "~> 2.0"}, 37 | {:phoenix, "~> 1.6.6"}, 38 | {:phoenix_ecto, "~> 4.4"}, 39 | {:ecto_sql, "~> 3.6"}, 40 | {:ecto_sqlite3, ">= 0.0.0"}, 41 | {:phoenix_html, "~> 3.0"}, 42 | {:phoenix_live_reload, "~> 1.2", only: :dev}, 43 | {:phoenix_live_view, "~> 0.17.5"}, 44 | {:floki, ">= 0.30.0", only: :test}, 45 | {:phoenix_live_dashboard, "~> 0.6"}, 46 | {:esbuild, "~> 0.3", runtime: Mix.env() == :dev}, 47 | {:swoosh, "~> 1.3"}, 48 | {:telemetry_metrics, "~> 0.6"}, 49 | {:telemetry_poller, "~> 1.0"}, 50 | {:gettext, "~> 0.18"}, 51 | {:jason, "~> 1.2"}, 52 | {:plug_cowboy, "~> 2.5"} 53 | ] 54 | end 55 | 56 | # Aliases are shortcuts or tasks specific to the current project. 57 | # For example, to install project dependencies and perform other setup tasks, run: 58 | # 59 | # $ mix setup 60 | # 61 | # See the documentation for `Mix` for more info on aliases. 62 | defp aliases do 63 | [ 64 | setup: ["deps.get", "ecto.setup"], 65 | "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"], 66 | "ecto.reset": ["ecto.drop", "ecto.setup"], 67 | test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"], 68 | "assets.deploy": ["esbuild default --minify", "phx.digest"] 69 | ] 70 | end 71 | end 72 | -------------------------------------------------------------------------------- /mix.lock: -------------------------------------------------------------------------------- 1 | %{ 2 | "bcrypt_elixir": {:hex, :bcrypt_elixir, "2.3.1", "5114d780459a04f2b4aeef52307de23de961b69e13a5cd98a911e39fda13f420", [:make, :mix], [{:comeonin, "~> 5.3", [hex: :comeonin, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "42182d5f46764def15bf9af83739e3bf4ad22661b1c34fc3e88558efced07279"}, 3 | "castore": {:hex, :castore, "0.1.15", "dbb300827d5a3ec48f396ca0b77ad47058578927e9ebe792abd99fcbc3324326", [:mix], [], "hexpm", "c69379b907673c7e6eb229f09a0a09b60bb27cfb9625bcb82ea4c04ba82a8442"}, 4 | "comeonin": {:hex, :comeonin, "5.3.3", "2c564dac95a35650e9b6acfe6d2952083d8a08e4a89b93a481acb552b325892e", [:mix], [], "hexpm", "3e38c9c2cb080828116597ca8807bb482618a315bfafd98c90bc22a821cc84df"}, 5 | "connection": {:hex, :connection, "1.1.0", "ff2a49c4b75b6fb3e674bfc5536451607270aac754ffd1bdfe175abe4a6d7a68", [:mix], [], "hexpm", "722c1eb0a418fbe91ba7bd59a47e28008a189d47e37e0e7bb85585a016b2869c"}, 6 | "cowboy": {:hex, :cowboy, "2.9.0", "865dd8b6607e14cf03282e10e934023a1bd8be6f6bacf921a7e2a96d800cd452", [:make, :rebar3], [{:cowlib, "2.11.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "2c729f934b4e1aa149aff882f57c6372c15399a20d54f65c8d67bef583021bde"}, 7 | "cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"}, 8 | "cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"}, 9 | "db_connection": {:hex, :db_connection, "2.4.1", "6411f6e23f1a8b68a82fa3a36366d4881f21f47fc79a9efb8c615e62050219da", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ea36d226ec5999781a9a8ad64e5d8c4454ecedc7a4d643e4832bf08efca01f00"}, 10 | "decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"}, 11 | "ecto": {:hex, :ecto, "3.7.1", "a20598862351b29f80f285b21ec5297da1181c0442687f9b8329f0445d228892", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d36e5b39fc479e654cffd4dbe1865d9716e4a9b6311faff799b6f90ab81b8638"}, 12 | "ecto_sql": {:hex, :ecto_sql, "3.7.2", "55c60aa3a06168912abf145c6df38b0295c34118c3624cf7a6977cd6ce043081", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.4.0 or ~> 0.5.0 or ~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0 or ~> 0.16.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3c218ea62f305dcaef0b915fb56583195e7b91c91dcfb006ba1f669bfacbff2a"}, 13 | "ecto_sqlite3": {:hex, :ecto_sqlite3, "0.7.3", "119e5142f23b9868ac17449cd945557897c18f30c0b39e3eb96659729d38310e", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.7", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:exqlite, "~> 0.9", [hex: :exqlite, repo: "hexpm", optional: false]}], "hexpm", "5a149b96e6c2e2ebcca60d23cbcf89130f7fbbcdba62956a70aa3d6d002a8e54"}, 14 | "elixir_make": {:hex, :elixir_make, "0.6.3", "bc07d53221216838d79e03a8019d0839786703129599e9619f4ab74c8c096eac", [:mix], [], "hexpm", "f5cbd651c5678bcaabdbb7857658ee106b12509cd976c2c2fca99688e1daf716"}, 15 | "esbuild": {:hex, :esbuild, "0.4.0", "9f17db148aead4cf1e6e6a584214357287a93407b5fb51a031f122b61385d4c2", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "b61e4e6b92ffe45e4ee4755a22de6211a67c67987dc02afb35a425a0add1d447"}, 16 | "exqlite": {:hex, :exqlite, "0.9.3", "57c80e742584dc4486d717681956d4152c7d03fb34ddbfb269844b504824528d", [:make, :mix], [{:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "5108f84bcc91fd7ae5b1b247e2be3860e449de5f8383ccaa1454278ffa1fc509"}, 17 | "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"}, 18 | "floki": {:hex, :floki, "0.32.0", "f915dc15258bc997d49be1f5ef7d3992f8834d6f5695270acad17b41f5bcc8e2", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "1c5a91cae1fd8931c26a4826b5e2372c284813904c8bacb468b5de39c7ececbd"}, 19 | "gettext": {:hex, :gettext, "0.19.1", "564953fd21f29358e68b91634799d9d26989f8d039d7512622efb3c3b1c97892", [:mix], [], "hexpm", "10c656c0912b8299adba9b061c06947511e3f109ab0d18b44a866a4498e77222"}, 20 | "html_entities": {:hex, :html_entities, "0.5.2", "9e47e70598da7de2a9ff6af8758399251db6dbb7eebe2b013f2bbd2515895c3c", [:mix], [], "hexpm", "c53ba390403485615623b9531e97696f076ed415e8d8058b1dbaa28181f4fdcc"}, 21 | "jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"}, 22 | "mime": {:hex, :mime, "2.0.2", "0b9e1a4c840eafb68d820b0e2158ef5c49385d17fb36855ac6e7e087d4b1dcc5", [:mix], [], "hexpm", "e6a3f76b4c277739e36c2e21a2c640778ba4c3846189d5ab19f97f126df5f9b7"}, 23 | "phoenix": {:hex, :phoenix, "1.6.6", "281c8ce8dccc9f60607346b72cdfc597c3dde134dd9df28dff08282f0b751754", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 1.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "807bd646e64cd9dc83db016199715faba72758e6db1de0707eef0a2da4924364"}, 24 | "phoenix_ecto": {:hex, :phoenix_ecto, "4.4.0", "0672ed4e4808b3fbed494dded89958e22fb882de47a97634c0b13e7b0b5f7720", [:mix], [{:ecto, "~> 3.3", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "09864e558ed31ee00bd48fcc1d4fc58ae9678c9e81649075431e69dbabb43cc1"}, 25 | "phoenix_html": {:hex, :phoenix_html, "3.2.0", "1c1219d4b6cb22ac72f12f73dc5fad6c7563104d083f711c3fcd8551a1f4ae11", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "36ec97ba56d25c0136ef1992c37957e4246b649d620958a1f9fa86165f8bc54f"}, 26 | "phoenix_live_dashboard": {:hex, :phoenix_live_dashboard, "0.6.5", "1495bb014be12c9a9252eca04b9af54246f6b5c1e4cd1f30210cd00ec540cf8e", [:mix], [{:ecto, "~> 3.6.2 or ~> 3.7", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_mysql_extras, "~> 0.3", [hex: :ecto_mysql_extras, repo: "hexpm", optional: true]}, {:ecto_psql_extras, "~> 0.7", [hex: :ecto_psql_extras, repo: "hexpm", optional: true]}, {:mime, "~> 1.6 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.17.7", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.6.0", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "ef4fa50dd78364409039c99cf6f98ab5209b4c5f8796c17f4db118324f0db852"}, 27 | "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.3.3", "3a53772a6118d5679bf50fc1670505a290e32a1d195df9e069d8c53ab040c054", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "766796676e5f558dbae5d1bdb066849673e956005e3730dfd5affd7a6da4abac"}, 28 | "phoenix_live_view": {:hex, :phoenix_live_view, "0.17.7", "05a42377075868a678d446361effba80cefef19ab98941c01a7a4c7560b29121", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.5.9 or ~> 1.6.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 3.1", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "25eaf41028eb351b90d4f69671874643a09944098fefd0d01d442f40a6091b6f"}, 29 | "phoenix_pubsub": {:hex, :phoenix_pubsub, "2.0.0", "a1ae76717bb168cdeb10ec9d92d1480fec99e3080f011402c0a2d68d47395ffb", [:mix], [], "hexpm", "c52d948c4f261577b9c6fa804be91884b381a7f8f18450c5045975435350f771"}, 30 | "phoenix_view": {:hex, :phoenix_view, "1.1.2", "1b82764a065fb41051637872c7bd07ed2fdb6f5c3bd89684d4dca6e10115c95a", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "7ae90ad27b09091266f6adbb61e1d2516a7c3d7062c6789d46a7554ec40f3a56"}, 31 | "plug": {:hex, :plug, "1.13.3", "93b299039c21a8b82cc904d13812bce4ced45cf69153e8d35ca16ffb3e8c5d98", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "98c8003e4faf7b74a9ac41bee99e328b08f069bf932747d4a7532e97ae837a17"}, 32 | "plug_cowboy": {:hex, :plug_cowboy, "2.5.2", "62894ccd601cf9597e2c23911ff12798a8a18d237e9739f58a6b04e4988899fe", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "ea6e87f774c8608d60c8d34022a7d073bd7680a0a013f049fc62bf35efea1044"}, 33 | "plug_crypto": {:hex, :plug_crypto, "1.2.2", "05654514ac717ff3a1843204b424477d9e60c143406aa94daf2274fdd280794d", [:mix], [], "hexpm", "87631c7ad914a5a445f0a3809f99b079113ae4ed4b867348dd9eec288cecb6db"}, 34 | "ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"}, 35 | "swoosh": {:hex, :swoosh, "1.6.3", "598d3f07641004bedb3eede40057760ae18be1073cff72f079ca1e1fc9cd97b9", [:mix], [{:cowboy, "~> 1.1 or ~> 2.4", [hex: :cowboy, repo: "hexpm", optional: true]}, {:finch, "~> 0.6", [hex: :finch, repo: "hexpm", optional: true]}, {:gen_smtp, "~> 0.13 or ~> 1.0", [hex: :gen_smtp, repo: "hexpm", optional: true]}, {:hackney, "~> 1.9", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mail, "~> 0.2", [hex: :mail, repo: "hexpm", optional: true]}, {:mime, "~> 1.1 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_cowboy, ">= 1.0.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "81ff9d7c7c4005a57465a7eb712edd71db51829aef94c8a34c30c5b9e9964adf"}, 36 | "telemetry": {:hex, :telemetry, "1.0.0", "0f453a102cdf13d506b7c0ab158324c337c41f1cc7548f0bc0e130bbf0ae9452", [:rebar3], [], "hexpm", "73bc09fa59b4a0284efb4624335583c528e07ec9ae76aca96ea0673850aec57a"}, 37 | "telemetry_metrics": {:hex, :telemetry_metrics, "0.6.1", "315d9163a1d4660aedc3fee73f33f1d355dcc76c5c3ab3d59e76e3edf80eef1f", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7be9e0871c41732c233be71e4be11b96e56177bf15dde64a8ac9ce72ac9834c6"}, 38 | "telemetry_poller": {:hex, :telemetry_poller, "1.0.0", "db91bb424e07f2bb6e73926fcafbfcbcb295f0193e0a00e825e589a0a47e8453", [:rebar3], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "b3a24eafd66c3f42da30fc3ca7dda1e9d546c12250a2d60d7b81d264fbec4f6e"}, 39 | } 40 | -------------------------------------------------------------------------------- /priv/gettext/en/LC_MESSAGES/errors.po: -------------------------------------------------------------------------------- 1 | ## `msgid`s in this file come from POT (.pot) files. 2 | ## 3 | ## Do not add, change, or remove `msgid`s manually here as 4 | ## they're tied to the ones in the corresponding POT file 5 | ## (with the same domain). 6 | ## 7 | ## Use `mix gettext.extract --merge` or `mix gettext.merge` 8 | ## to merge POT files into PO files. 9 | msgid "" 10 | msgstr "" 11 | "Language: en\n" 12 | 13 | ## From Ecto.Changeset.cast/4 14 | msgid "can't be blank" 15 | msgstr "" 16 | 17 | ## From Ecto.Changeset.unique_constraint/3 18 | msgid "has already been taken" 19 | msgstr "" 20 | 21 | ## From Ecto.Changeset.put_change/3 22 | msgid "is invalid" 23 | msgstr "" 24 | 25 | ## From Ecto.Changeset.validate_acceptance/3 26 | msgid "must be accepted" 27 | msgstr "" 28 | 29 | ## From Ecto.Changeset.validate_format/3 30 | msgid "has invalid format" 31 | msgstr "" 32 | 33 | ## From Ecto.Changeset.validate_subset/3 34 | msgid "has an invalid entry" 35 | msgstr "" 36 | 37 | ## From Ecto.Changeset.validate_exclusion/3 38 | msgid "is reserved" 39 | msgstr "" 40 | 41 | ## From Ecto.Changeset.validate_confirmation/3 42 | msgid "does not match confirmation" 43 | msgstr "" 44 | 45 | ## From Ecto.Changeset.no_assoc_constraint/3 46 | msgid "is still associated with this entry" 47 | msgstr "" 48 | 49 | msgid "are still associated with this entry" 50 | msgstr "" 51 | 52 | ## From Ecto.Changeset.validate_length/3 53 | msgid "should have %{count} item(s)" 54 | msgid_plural "should have %{count} item(s)" 55 | msgstr[0] "" 56 | msgstr[1] "" 57 | 58 | msgid "should be %{count} character(s)" 59 | msgid_plural "should be %{count} character(s)" 60 | msgstr[0] "" 61 | msgstr[1] "" 62 | 63 | msgid "should be %{count} byte(s)" 64 | msgid_plural "should be %{count} byte(s)" 65 | msgstr[0] "" 66 | msgstr[1] "" 67 | 68 | msgid "should have at least %{count} item(s)" 69 | msgid_plural "should have at least %{count} item(s)" 70 | msgstr[0] "" 71 | msgstr[1] "" 72 | 73 | msgid "should be at least %{count} character(s)" 74 | msgid_plural "should be at least %{count} character(s)" 75 | msgstr[0] "" 76 | msgstr[1] "" 77 | 78 | msgid "should be at least %{count} byte(s)" 79 | msgid_plural "should be at least %{count} byte(s)" 80 | msgstr[0] "" 81 | msgstr[1] "" 82 | 83 | msgid "should have at most %{count} item(s)" 84 | msgid_plural "should have at most %{count} item(s)" 85 | msgstr[0] "" 86 | msgstr[1] "" 87 | 88 | msgid "should be at most %{count} character(s)" 89 | msgid_plural "should be at most %{count} character(s)" 90 | msgstr[0] "" 91 | msgstr[1] "" 92 | 93 | msgid "should be at most %{count} byte(s)" 94 | msgid_plural "should be at most %{count} byte(s)" 95 | msgstr[0] "" 96 | msgstr[1] "" 97 | 98 | ## From Ecto.Changeset.validate_number/3 99 | msgid "must be less than %{number}" 100 | msgstr "" 101 | 102 | msgid "must be greater than %{number}" 103 | msgstr "" 104 | 105 | msgid "must be less than or equal to %{number}" 106 | msgstr "" 107 | 108 | msgid "must be greater than or equal to %{number}" 109 | msgstr "" 110 | 111 | msgid "must be equal to %{number}" 112 | msgstr "" 113 | -------------------------------------------------------------------------------- /priv/gettext/errors.pot: -------------------------------------------------------------------------------- 1 | ## This is a PO Template file. 2 | ## 3 | ## `msgid`s here are often extracted from source code. 4 | ## Add new translations manually only if they're dynamic 5 | ## translations that can't be statically extracted. 6 | ## 7 | ## Run `mix gettext.extract` to bring this file up to 8 | ## date. Leave `msgstr`s empty as changing them here has no 9 | ## effect: edit them in PO (`.po`) files instead. 10 | 11 | ## From Ecto.Changeset.cast/4 12 | msgid "can't be blank" 13 | msgstr "" 14 | 15 | ## From Ecto.Changeset.unique_constraint/3 16 | msgid "has already been taken" 17 | msgstr "" 18 | 19 | ## From Ecto.Changeset.put_change/3 20 | msgid "is invalid" 21 | msgstr "" 22 | 23 | ## From Ecto.Changeset.validate_acceptance/3 24 | msgid "must be accepted" 25 | msgstr "" 26 | 27 | ## From Ecto.Changeset.validate_format/3 28 | msgid "has invalid format" 29 | msgstr "" 30 | 31 | ## From Ecto.Changeset.validate_subset/3 32 | msgid "has an invalid entry" 33 | msgstr "" 34 | 35 | ## From Ecto.Changeset.validate_exclusion/3 36 | msgid "is reserved" 37 | msgstr "" 38 | 39 | ## From Ecto.Changeset.validate_confirmation/3 40 | msgid "does not match confirmation" 41 | msgstr "" 42 | 43 | ## From Ecto.Changeset.no_assoc_constraint/3 44 | msgid "is still associated with this entry" 45 | msgstr "" 46 | 47 | msgid "are still associated with this entry" 48 | msgstr "" 49 | 50 | ## From Ecto.Changeset.validate_length/3 51 | msgid "should be %{count} character(s)" 52 | msgid_plural "should be %{count} character(s)" 53 | msgstr[0] "" 54 | msgstr[1] "" 55 | 56 | msgid "should have %{count} item(s)" 57 | msgid_plural "should have %{count} item(s)" 58 | msgstr[0] "" 59 | msgstr[1] "" 60 | 61 | msgid "should be at least %{count} character(s)" 62 | msgid_plural "should be at least %{count} character(s)" 63 | msgstr[0] "" 64 | msgstr[1] "" 65 | 66 | msgid "should have at least %{count} item(s)" 67 | msgid_plural "should have at least %{count} item(s)" 68 | msgstr[0] "" 69 | msgstr[1] "" 70 | 71 | msgid "should be at most %{count} character(s)" 72 | msgid_plural "should be at most %{count} character(s)" 73 | msgstr[0] "" 74 | msgstr[1] "" 75 | 76 | msgid "should have at most %{count} item(s)" 77 | msgid_plural "should have at most %{count} item(s)" 78 | msgstr[0] "" 79 | msgstr[1] "" 80 | 81 | ## From Ecto.Changeset.validate_number/3 82 | msgid "must be less than %{number}" 83 | msgstr "" 84 | 85 | msgid "must be greater than %{number}" 86 | msgstr "" 87 | 88 | msgid "must be less than or equal to %{number}" 89 | msgstr "" 90 | 91 | msgid "must be greater than or equal to %{number}" 92 | msgstr "" 93 | 94 | msgid "must be equal to %{number}" 95 | msgstr "" 96 | -------------------------------------------------------------------------------- /priv/repo/migrations/.formatter.exs: -------------------------------------------------------------------------------- 1 | [ 2 | import_deps: [:ecto_sql], 3 | inputs: ["*.exs"] 4 | ] 5 | -------------------------------------------------------------------------------- /priv/repo/migrations/20220221061920_create_users_auth_tables.exs: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.Repo.Migrations.CreateUsersAuthTables do 2 | use Ecto.Migration 3 | 4 | def change do 5 | create table(:users, primary_key: false) do 6 | add :id, :binary_id, primary_key: true 7 | add :email, :string, null: false, collate: :nocase 8 | add :hashed_password, :string, null: false 9 | add :confirmed_at, :naive_datetime 10 | timestamps() 11 | end 12 | 13 | create unique_index(:users, [:email]) 14 | 15 | create table(:users_tokens, primary_key: false) do 16 | add :id, :binary_id, primary_key: true 17 | add :user_id, references(:users, type: :binary_id, on_delete: :delete_all), null: false 18 | add :token, :binary, null: false, size: 32 19 | add :context, :string, null: false 20 | add :sent_to, :string 21 | timestamps(updated_at: false) 22 | end 23 | 24 | create index(:users_tokens, [:user_id]) 25 | create unique_index(:users_tokens, [:context, :token]) 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /priv/repo/seeds.exs: -------------------------------------------------------------------------------- 1 | # Script for populating the database. You can run it as: 2 | # 3 | # mix run priv/repo/seeds.exs 4 | # 5 | # Inside the script, you can read and write to any of your 6 | # repositories directly: 7 | # 8 | # SqliteScale.Repo.insert!(%SqliteScale.SomeSchema{}) 9 | # 10 | # We recommend using the bang functions (`insert!`, `update!` 11 | # and so on) as they will fail if something goes wrong. 12 | -------------------------------------------------------------------------------- /priv/static/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akoutmos/sqlite_scale/b136972322c2e7fb1db0af2cad9e16d7ef22a975/priv/static/favicon.ico -------------------------------------------------------------------------------- /priv/static/images/phoenix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akoutmos/sqlite_scale/b136972322c2e7fb1db0af2cad9e16d7ef22a975/priv/static/images/phoenix.png -------------------------------------------------------------------------------- /priv/static/robots.txt: -------------------------------------------------------------------------------- 1 | # See https://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file 2 | # 3 | # To ban all spiders from the entire site uncomment the next two lines: 4 | # User-agent: * 5 | # Disallow: / 6 | -------------------------------------------------------------------------------- /priv/user_repo/migrations/20220221192607_add_todo_list_table.exs: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.UserRepo.Migrations.AddTodoListTable do 2 | use Ecto.Migration 3 | 4 | def change do 5 | create table(:todo_items, primary_key: false) do 6 | add(:id, :binary_id, primary_key: true) 7 | add(:item, :string, null: false) 8 | 9 | timestamps() 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /test/sqlite_scale/accounts_test.exs: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.AccountsTest do 2 | use SqliteScale.DataCase 3 | 4 | alias SqliteScale.Accounts 5 | 6 | import SqliteScale.AccountsFixtures 7 | alias SqliteScale.Accounts.{User, UserToken} 8 | 9 | describe "get_user_by_email/1" do 10 | test "does not return the user if the email does not exist" do 11 | refute Accounts.get_user_by_email("unknown@example.com") 12 | end 13 | 14 | test "returns the user if the email exists" do 15 | %{id: id} = user = user_fixture() 16 | assert %User{id: ^id} = Accounts.get_user_by_email(user.email) 17 | end 18 | end 19 | 20 | describe "get_user_by_email_and_password/2" do 21 | test "does not return the user if the email does not exist" do 22 | refute Accounts.get_user_by_email_and_password("unknown@example.com", "hello world!") 23 | end 24 | 25 | test "does not return the user if the password is not valid" do 26 | user = user_fixture() 27 | refute Accounts.get_user_by_email_and_password(user.email, "invalid") 28 | end 29 | 30 | test "returns the user if the email and password are valid" do 31 | %{id: id} = user = user_fixture() 32 | 33 | assert %User{id: ^id} = 34 | Accounts.get_user_by_email_and_password(user.email, valid_user_password()) 35 | end 36 | end 37 | 38 | describe "get_user!/1" do 39 | test "raises if id is invalid" do 40 | assert_raise Ecto.NoResultsError, fn -> 41 | Accounts.get_user!("11111111-1111-1111-1111-111111111111") 42 | end 43 | end 44 | 45 | test "returns the user with the given id" do 46 | %{id: id} = user = user_fixture() 47 | assert %User{id: ^id} = Accounts.get_user!(user.id) 48 | end 49 | end 50 | 51 | describe "register_user/1" do 52 | test "requires email and password to be set" do 53 | {:error, changeset} = Accounts.register_user(%{}) 54 | 55 | assert %{ 56 | password: ["can't be blank"], 57 | email: ["can't be blank"] 58 | } = errors_on(changeset) 59 | end 60 | 61 | test "validates email and password when given" do 62 | {:error, changeset} = Accounts.register_user(%{email: "not valid", password: "not valid"}) 63 | 64 | assert %{ 65 | email: ["must have the @ sign and no spaces"], 66 | password: ["should be at least 12 character(s)"] 67 | } = errors_on(changeset) 68 | end 69 | 70 | test "validates maximum values for email and password for security" do 71 | too_long = String.duplicate("db", 100) 72 | {:error, changeset} = Accounts.register_user(%{email: too_long, password: too_long}) 73 | assert "should be at most 160 character(s)" in errors_on(changeset).email 74 | assert "should be at most 72 character(s)" in errors_on(changeset).password 75 | end 76 | 77 | test "validates email uniqueness" do 78 | %{email: email} = user_fixture() 79 | {:error, changeset} = Accounts.register_user(%{email: email}) 80 | assert "has already been taken" in errors_on(changeset).email 81 | 82 | # Now try with the upper cased email too, to check that email case is ignored. 83 | {:error, changeset} = Accounts.register_user(%{email: String.upcase(email)}) 84 | assert "has already been taken" in errors_on(changeset).email 85 | end 86 | 87 | test "registers users with a hashed password" do 88 | email = unique_user_email() 89 | {:ok, user} = Accounts.register_user(valid_user_attributes(email: email)) 90 | assert user.email == email 91 | assert is_binary(user.hashed_password) 92 | assert is_nil(user.confirmed_at) 93 | assert is_nil(user.password) 94 | end 95 | end 96 | 97 | describe "change_user_registration/2" do 98 | test "returns a changeset" do 99 | assert %Ecto.Changeset{} = changeset = Accounts.change_user_registration(%User{}) 100 | assert changeset.required == [:password, :email] 101 | end 102 | 103 | test "allows fields to be set" do 104 | email = unique_user_email() 105 | password = valid_user_password() 106 | 107 | changeset = 108 | Accounts.change_user_registration( 109 | %User{}, 110 | valid_user_attributes(email: email, password: password) 111 | ) 112 | 113 | assert changeset.valid? 114 | assert get_change(changeset, :email) == email 115 | assert get_change(changeset, :password) == password 116 | assert is_nil(get_change(changeset, :hashed_password)) 117 | end 118 | end 119 | 120 | describe "change_user_email/2" do 121 | test "returns a user changeset" do 122 | assert %Ecto.Changeset{} = changeset = Accounts.change_user_email(%User{}) 123 | assert changeset.required == [:email] 124 | end 125 | end 126 | 127 | describe "apply_user_email/3" do 128 | setup do 129 | %{user: user_fixture()} 130 | end 131 | 132 | test "requires email to change", %{user: user} do 133 | {:error, changeset} = Accounts.apply_user_email(user, valid_user_password(), %{}) 134 | assert %{email: ["did not change"]} = errors_on(changeset) 135 | end 136 | 137 | test "validates email", %{user: user} do 138 | {:error, changeset} = 139 | Accounts.apply_user_email(user, valid_user_password(), %{email: "not valid"}) 140 | 141 | assert %{email: ["must have the @ sign and no spaces"]} = errors_on(changeset) 142 | end 143 | 144 | test "validates maximum value for email for security", %{user: user} do 145 | too_long = String.duplicate("db", 100) 146 | 147 | {:error, changeset} = 148 | Accounts.apply_user_email(user, valid_user_password(), %{email: too_long}) 149 | 150 | assert "should be at most 160 character(s)" in errors_on(changeset).email 151 | end 152 | 153 | test "validates email uniqueness", %{user: user} do 154 | %{email: email} = user_fixture() 155 | 156 | {:error, changeset} = 157 | Accounts.apply_user_email(user, valid_user_password(), %{email: email}) 158 | 159 | assert "has already been taken" in errors_on(changeset).email 160 | end 161 | 162 | test "validates current password", %{user: user} do 163 | {:error, changeset} = 164 | Accounts.apply_user_email(user, "invalid", %{email: unique_user_email()}) 165 | 166 | assert %{current_password: ["is not valid"]} = errors_on(changeset) 167 | end 168 | 169 | test "applies the email without persisting it", %{user: user} do 170 | email = unique_user_email() 171 | {:ok, user} = Accounts.apply_user_email(user, valid_user_password(), %{email: email}) 172 | assert user.email == email 173 | assert Accounts.get_user!(user.id).email != email 174 | end 175 | end 176 | 177 | describe "deliver_update_email_instructions/3" do 178 | setup do 179 | %{user: user_fixture()} 180 | end 181 | 182 | test "sends token through notification", %{user: user} do 183 | token = 184 | extract_user_token(fn url -> 185 | Accounts.deliver_update_email_instructions(user, "current@example.com", url) 186 | end) 187 | 188 | {:ok, token} = Base.url_decode64(token, padding: false) 189 | assert user_token = Repo.get_by(UserToken, token: :crypto.hash(:sha256, token)) 190 | assert user_token.user_id == user.id 191 | assert user_token.sent_to == user.email 192 | assert user_token.context == "change:current@example.com" 193 | end 194 | end 195 | 196 | describe "update_user_email/2" do 197 | setup do 198 | user = user_fixture() 199 | email = unique_user_email() 200 | 201 | token = 202 | extract_user_token(fn url -> 203 | Accounts.deliver_update_email_instructions(%{user | email: email}, user.email, url) 204 | end) 205 | 206 | %{user: user, token: token, email: email} 207 | end 208 | 209 | test "updates the email with a valid token", %{user: user, token: token, email: email} do 210 | assert Accounts.update_user_email(user, token) == :ok 211 | changed_user = Repo.get!(User, user.id) 212 | assert changed_user.email != user.email 213 | assert changed_user.email == email 214 | assert changed_user.confirmed_at 215 | assert changed_user.confirmed_at != user.confirmed_at 216 | refute Repo.get_by(UserToken, user_id: user.id) 217 | end 218 | 219 | test "does not update email with invalid token", %{user: user} do 220 | assert Accounts.update_user_email(user, "oops") == :error 221 | assert Repo.get!(User, user.id).email == user.email 222 | assert Repo.get_by(UserToken, user_id: user.id) 223 | end 224 | 225 | test "does not update email if user email changed", %{user: user, token: token} do 226 | assert Accounts.update_user_email(%{user | email: "current@example.com"}, token) == :error 227 | assert Repo.get!(User, user.id).email == user.email 228 | assert Repo.get_by(UserToken, user_id: user.id) 229 | end 230 | 231 | test "does not update email if token expired", %{user: user, token: token} do 232 | {1, nil} = Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]]) 233 | assert Accounts.update_user_email(user, token) == :error 234 | assert Repo.get!(User, user.id).email == user.email 235 | assert Repo.get_by(UserToken, user_id: user.id) 236 | end 237 | end 238 | 239 | describe "change_user_password/2" do 240 | test "returns a user changeset" do 241 | assert %Ecto.Changeset{} = changeset = Accounts.change_user_password(%User{}) 242 | assert changeset.required == [:password] 243 | end 244 | 245 | test "allows fields to be set" do 246 | changeset = 247 | Accounts.change_user_password(%User{}, %{ 248 | "password" => "new valid password" 249 | }) 250 | 251 | assert changeset.valid? 252 | assert get_change(changeset, :password) == "new valid password" 253 | assert is_nil(get_change(changeset, :hashed_password)) 254 | end 255 | end 256 | 257 | describe "update_user_password/3" do 258 | setup do 259 | %{user: user_fixture()} 260 | end 261 | 262 | test "validates password", %{user: user} do 263 | {:error, changeset} = 264 | Accounts.update_user_password(user, valid_user_password(), %{ 265 | password: "not valid", 266 | password_confirmation: "another" 267 | }) 268 | 269 | assert %{ 270 | password: ["should be at least 12 character(s)"], 271 | password_confirmation: ["does not match password"] 272 | } = errors_on(changeset) 273 | end 274 | 275 | test "validates maximum values for password for security", %{user: user} do 276 | too_long = String.duplicate("db", 100) 277 | 278 | {:error, changeset} = 279 | Accounts.update_user_password(user, valid_user_password(), %{password: too_long}) 280 | 281 | assert "should be at most 72 character(s)" in errors_on(changeset).password 282 | end 283 | 284 | test "validates current password", %{user: user} do 285 | {:error, changeset} = 286 | Accounts.update_user_password(user, "invalid", %{password: valid_user_password()}) 287 | 288 | assert %{current_password: ["is not valid"]} = errors_on(changeset) 289 | end 290 | 291 | test "updates the password", %{user: user} do 292 | {:ok, user} = 293 | Accounts.update_user_password(user, valid_user_password(), %{ 294 | password: "new valid password" 295 | }) 296 | 297 | assert is_nil(user.password) 298 | assert Accounts.get_user_by_email_and_password(user.email, "new valid password") 299 | end 300 | 301 | test "deletes all tokens for the given user", %{user: user} do 302 | _ = Accounts.generate_user_session_token(user) 303 | 304 | {:ok, _} = 305 | Accounts.update_user_password(user, valid_user_password(), %{ 306 | password: "new valid password" 307 | }) 308 | 309 | refute Repo.get_by(UserToken, user_id: user.id) 310 | end 311 | end 312 | 313 | describe "generate_user_session_token/1" do 314 | setup do 315 | %{user: user_fixture()} 316 | end 317 | 318 | test "generates a token", %{user: user} do 319 | token = Accounts.generate_user_session_token(user) 320 | assert user_token = Repo.get_by(UserToken, token: token) 321 | assert user_token.context == "session" 322 | 323 | # Creating the same token for another user should fail 324 | assert_raise Ecto.ConstraintError, fn -> 325 | Repo.insert!(%UserToken{ 326 | token: user_token.token, 327 | user_id: user_fixture().id, 328 | context: "session" 329 | }) 330 | end 331 | end 332 | end 333 | 334 | describe "get_user_by_session_token/1" do 335 | setup do 336 | user = user_fixture() 337 | token = Accounts.generate_user_session_token(user) 338 | %{user: user, token: token} 339 | end 340 | 341 | test "returns user by token", %{user: user, token: token} do 342 | assert session_user = Accounts.get_user_by_session_token(token) 343 | assert session_user.id == user.id 344 | end 345 | 346 | test "does not return user for invalid token" do 347 | refute Accounts.get_user_by_session_token("oops") 348 | end 349 | 350 | test "does not return user for expired token", %{token: token} do 351 | {1, nil} = Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]]) 352 | refute Accounts.get_user_by_session_token(token) 353 | end 354 | end 355 | 356 | describe "delete_session_token/1" do 357 | test "deletes the token" do 358 | user = user_fixture() 359 | token = Accounts.generate_user_session_token(user) 360 | assert Accounts.delete_session_token(token) == :ok 361 | refute Accounts.get_user_by_session_token(token) 362 | end 363 | end 364 | 365 | describe "deliver_user_confirmation_instructions/2" do 366 | setup do 367 | %{user: user_fixture()} 368 | end 369 | 370 | test "sends token through notification", %{user: user} do 371 | token = 372 | extract_user_token(fn url -> 373 | Accounts.deliver_user_confirmation_instructions(user, url) 374 | end) 375 | 376 | {:ok, token} = Base.url_decode64(token, padding: false) 377 | assert user_token = Repo.get_by(UserToken, token: :crypto.hash(:sha256, token)) 378 | assert user_token.user_id == user.id 379 | assert user_token.sent_to == user.email 380 | assert user_token.context == "confirm" 381 | end 382 | end 383 | 384 | describe "confirm_user/1" do 385 | setup do 386 | user = user_fixture() 387 | 388 | token = 389 | extract_user_token(fn url -> 390 | Accounts.deliver_user_confirmation_instructions(user, url) 391 | end) 392 | 393 | %{user: user, token: token} 394 | end 395 | 396 | test "confirms the email with a valid token", %{user: user, token: token} do 397 | assert {:ok, confirmed_user} = Accounts.confirm_user(token) 398 | assert confirmed_user.confirmed_at 399 | assert confirmed_user.confirmed_at != user.confirmed_at 400 | assert Repo.get!(User, user.id).confirmed_at 401 | refute Repo.get_by(UserToken, user_id: user.id) 402 | end 403 | 404 | test "does not confirm with invalid token", %{user: user} do 405 | assert Accounts.confirm_user("oops") == :error 406 | refute Repo.get!(User, user.id).confirmed_at 407 | assert Repo.get_by(UserToken, user_id: user.id) 408 | end 409 | 410 | test "does not confirm email if token expired", %{user: user, token: token} do 411 | {1, nil} = Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]]) 412 | assert Accounts.confirm_user(token) == :error 413 | refute Repo.get!(User, user.id).confirmed_at 414 | assert Repo.get_by(UserToken, user_id: user.id) 415 | end 416 | end 417 | 418 | describe "deliver_user_reset_password_instructions/2" do 419 | setup do 420 | %{user: user_fixture()} 421 | end 422 | 423 | test "sends token through notification", %{user: user} do 424 | token = 425 | extract_user_token(fn url -> 426 | Accounts.deliver_user_reset_password_instructions(user, url) 427 | end) 428 | 429 | {:ok, token} = Base.url_decode64(token, padding: false) 430 | assert user_token = Repo.get_by(UserToken, token: :crypto.hash(:sha256, token)) 431 | assert user_token.user_id == user.id 432 | assert user_token.sent_to == user.email 433 | assert user_token.context == "reset_password" 434 | end 435 | end 436 | 437 | describe "get_user_by_reset_password_token/1" do 438 | setup do 439 | user = user_fixture() 440 | 441 | token = 442 | extract_user_token(fn url -> 443 | Accounts.deliver_user_reset_password_instructions(user, url) 444 | end) 445 | 446 | %{user: user, token: token} 447 | end 448 | 449 | test "returns the user with valid token", %{user: %{id: id}, token: token} do 450 | assert %User{id: ^id} = Accounts.get_user_by_reset_password_token(token) 451 | assert Repo.get_by(UserToken, user_id: id) 452 | end 453 | 454 | test "does not return the user with invalid token", %{user: user} do 455 | refute Accounts.get_user_by_reset_password_token("oops") 456 | assert Repo.get_by(UserToken, user_id: user.id) 457 | end 458 | 459 | test "does not return the user if token expired", %{user: user, token: token} do 460 | {1, nil} = Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]]) 461 | refute Accounts.get_user_by_reset_password_token(token) 462 | assert Repo.get_by(UserToken, user_id: user.id) 463 | end 464 | end 465 | 466 | describe "reset_user_password/2" do 467 | setup do 468 | %{user: user_fixture()} 469 | end 470 | 471 | test "validates password", %{user: user} do 472 | {:error, changeset} = 473 | Accounts.reset_user_password(user, %{ 474 | password: "not valid", 475 | password_confirmation: "another" 476 | }) 477 | 478 | assert %{ 479 | password: ["should be at least 12 character(s)"], 480 | password_confirmation: ["does not match password"] 481 | } = errors_on(changeset) 482 | end 483 | 484 | test "validates maximum values for password for security", %{user: user} do 485 | too_long = String.duplicate("db", 100) 486 | {:error, changeset} = Accounts.reset_user_password(user, %{password: too_long}) 487 | assert "should be at most 72 character(s)" in errors_on(changeset).password 488 | end 489 | 490 | test "updates the password", %{user: user} do 491 | {:ok, updated_user} = Accounts.reset_user_password(user, %{password: "new valid password"}) 492 | assert is_nil(updated_user.password) 493 | assert Accounts.get_user_by_email_and_password(user.email, "new valid password") 494 | end 495 | 496 | test "deletes all tokens for the given user", %{user: user} do 497 | _ = Accounts.generate_user_session_token(user) 498 | {:ok, _} = Accounts.reset_user_password(user, %{password: "new valid password"}) 499 | refute Repo.get_by(UserToken, user_id: user.id) 500 | end 501 | end 502 | 503 | describe "inspect/2" do 504 | test "does not include password" do 505 | refute inspect(%User{password: "123456"}) =~ "password: \"123456\"" 506 | end 507 | end 508 | end 509 | -------------------------------------------------------------------------------- /test/sqlite_scale/todo_items_test.exs: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.TodoItemsTest do 2 | use SqliteScale.DataCase 3 | 4 | alias SqliteScale.TodoItems 5 | 6 | describe "todo_items" do 7 | alias SqliteScale.TodoItems.TodoItem 8 | 9 | import SqliteScale.TodoItemsFixtures 10 | 11 | @invalid_attrs %{item: nil} 12 | 13 | test "list_todo_items/0 returns all todo_items" do 14 | todo_item = todo_item_fixture() 15 | assert TodoItems.list_todo_items() == [todo_item] 16 | end 17 | 18 | test "get_todo_item!/1 returns the todo_item with given id" do 19 | todo_item = todo_item_fixture() 20 | assert TodoItems.get_todo_item!(todo_item.id) == todo_item 21 | end 22 | 23 | test "create_todo_item/1 with valid data creates a todo_item" do 24 | valid_attrs = %{item: "some item"} 25 | 26 | assert {:ok, %TodoItem{} = todo_item} = TodoItems.create_todo_item(valid_attrs) 27 | assert todo_item.item == "some item" 28 | end 29 | 30 | test "create_todo_item/1 with invalid data returns error changeset" do 31 | assert {:error, %Ecto.Changeset{}} = TodoItems.create_todo_item(@invalid_attrs) 32 | end 33 | 34 | test "update_todo_item/2 with valid data updates the todo_item" do 35 | todo_item = todo_item_fixture() 36 | update_attrs = %{item: "some updated item"} 37 | 38 | assert {:ok, %TodoItem{} = todo_item} = TodoItems.update_todo_item(todo_item, update_attrs) 39 | assert todo_item.item == "some updated item" 40 | end 41 | 42 | test "update_todo_item/2 with invalid data returns error changeset" do 43 | todo_item = todo_item_fixture() 44 | assert {:error, %Ecto.Changeset{}} = TodoItems.update_todo_item(todo_item, @invalid_attrs) 45 | assert todo_item == TodoItems.get_todo_item!(todo_item.id) 46 | end 47 | 48 | test "delete_todo_item/1 deletes the todo_item" do 49 | todo_item = todo_item_fixture() 50 | assert {:ok, %TodoItem{}} = TodoItems.delete_todo_item(todo_item) 51 | assert_raise Ecto.NoResultsError, fn -> TodoItems.get_todo_item!(todo_item.id) end 52 | end 53 | 54 | test "change_todo_item/1 returns a todo_item changeset" do 55 | todo_item = todo_item_fixture() 56 | assert %Ecto.Changeset{} = TodoItems.change_todo_item(todo_item) 57 | end 58 | end 59 | end 60 | -------------------------------------------------------------------------------- /test/sqlite_scale_web/controllers/page_controller_test.exs: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.PageControllerTest do 2 | use SqliteScaleWeb.ConnCase 3 | 4 | test "GET /", %{conn: conn} do 5 | conn = get(conn, "/") 6 | assert html_response(conn, 200) =~ "Welcome to Phoenix!" 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /test/sqlite_scale_web/controllers/todo_item_controller_test.exs: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.TodoItemControllerTest do 2 | use SqliteScaleWeb.ConnCase 3 | 4 | import SqliteScale.TodoItemsFixtures 5 | 6 | @create_attrs %{item: "some item"} 7 | @update_attrs %{item: "some updated item"} 8 | @invalid_attrs %{item: nil} 9 | 10 | describe "index" do 11 | test "lists all todo_items", %{conn: conn} do 12 | conn = get(conn, Routes.todo_item_path(conn, :index)) 13 | assert html_response(conn, 200) =~ "Listing Todo items" 14 | end 15 | end 16 | 17 | describe "new todo_item" do 18 | test "renders form", %{conn: conn} do 19 | conn = get(conn, Routes.todo_item_path(conn, :new)) 20 | assert html_response(conn, 200) =~ "New Todo item" 21 | end 22 | end 23 | 24 | describe "create todo_item" do 25 | test "redirects to show when data is valid", %{conn: conn} do 26 | conn = post(conn, Routes.todo_item_path(conn, :create), todo_item: @create_attrs) 27 | 28 | assert %{id: id} = redirected_params(conn) 29 | assert redirected_to(conn) == Routes.todo_item_path(conn, :show, id) 30 | 31 | conn = get(conn, Routes.todo_item_path(conn, :show, id)) 32 | assert html_response(conn, 200) =~ "Show Todo item" 33 | end 34 | 35 | test "renders errors when data is invalid", %{conn: conn} do 36 | conn = post(conn, Routes.todo_item_path(conn, :create), todo_item: @invalid_attrs) 37 | assert html_response(conn, 200) =~ "New Todo item" 38 | end 39 | end 40 | 41 | describe "edit todo_item" do 42 | setup [:create_todo_item] 43 | 44 | test "renders form for editing chosen todo_item", %{conn: conn, todo_item: todo_item} do 45 | conn = get(conn, Routes.todo_item_path(conn, :edit, todo_item)) 46 | assert html_response(conn, 200) =~ "Edit Todo item" 47 | end 48 | end 49 | 50 | describe "update todo_item" do 51 | setup [:create_todo_item] 52 | 53 | test "redirects when data is valid", %{conn: conn, todo_item: todo_item} do 54 | conn = put(conn, Routes.todo_item_path(conn, :update, todo_item), todo_item: @update_attrs) 55 | assert redirected_to(conn) == Routes.todo_item_path(conn, :show, todo_item) 56 | 57 | conn = get(conn, Routes.todo_item_path(conn, :show, todo_item)) 58 | assert html_response(conn, 200) =~ "some updated item" 59 | end 60 | 61 | test "renders errors when data is invalid", %{conn: conn, todo_item: todo_item} do 62 | conn = put(conn, Routes.todo_item_path(conn, :update, todo_item), todo_item: @invalid_attrs) 63 | assert html_response(conn, 200) =~ "Edit Todo item" 64 | end 65 | end 66 | 67 | describe "delete todo_item" do 68 | setup [:create_todo_item] 69 | 70 | test "deletes chosen todo_item", %{conn: conn, todo_item: todo_item} do 71 | conn = delete(conn, Routes.todo_item_path(conn, :delete, todo_item)) 72 | assert redirected_to(conn) == Routes.todo_item_path(conn, :index) 73 | 74 | assert_error_sent 404, fn -> 75 | get(conn, Routes.todo_item_path(conn, :show, todo_item)) 76 | end 77 | end 78 | end 79 | 80 | defp create_todo_item(_) do 81 | todo_item = todo_item_fixture() 82 | %{todo_item: todo_item} 83 | end 84 | end 85 | -------------------------------------------------------------------------------- /test/sqlite_scale_web/controllers/user_auth_test.exs: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.UserAuthTest do 2 | use SqliteScaleWeb.ConnCase 3 | 4 | alias SqliteScale.Accounts 5 | alias SqliteScaleWeb.UserAuth 6 | import SqliteScale.AccountsFixtures 7 | 8 | @remember_me_cookie "_sqlite_scale_web_user_remember_me" 9 | 10 | setup %{conn: conn} do 11 | conn = 12 | conn 13 | |> Map.replace!(:secret_key_base, SqliteScaleWeb.Endpoint.config(:secret_key_base)) 14 | |> init_test_session(%{}) 15 | 16 | %{user: user_fixture(), conn: conn} 17 | end 18 | 19 | describe "log_in_user/3" do 20 | test "stores the user token in the session", %{conn: conn, user: user} do 21 | conn = UserAuth.log_in_user(conn, user) 22 | assert token = get_session(conn, :user_token) 23 | assert get_session(conn, :live_socket_id) == "users_sessions:#{Base.url_encode64(token)}" 24 | assert redirected_to(conn) == "/" 25 | assert Accounts.get_user_by_session_token(token) 26 | end 27 | 28 | test "clears everything previously stored in the session", %{conn: conn, user: user} do 29 | conn = conn |> put_session(:to_be_removed, "value") |> UserAuth.log_in_user(user) 30 | refute get_session(conn, :to_be_removed) 31 | end 32 | 33 | test "redirects to the configured path", %{conn: conn, user: user} do 34 | conn = conn |> put_session(:user_return_to, "/hello") |> UserAuth.log_in_user(user) 35 | assert redirected_to(conn) == "/hello" 36 | end 37 | 38 | test "writes a cookie if remember_me is configured", %{conn: conn, user: user} do 39 | conn = conn |> fetch_cookies() |> UserAuth.log_in_user(user, %{"remember_me" => "true"}) 40 | assert get_session(conn, :user_token) == conn.cookies[@remember_me_cookie] 41 | 42 | assert %{value: signed_token, max_age: max_age} = conn.resp_cookies[@remember_me_cookie] 43 | assert signed_token != get_session(conn, :user_token) 44 | assert max_age == 5_184_000 45 | end 46 | end 47 | 48 | describe "logout_user/1" do 49 | test "erases session and cookies", %{conn: conn, user: user} do 50 | user_token = Accounts.generate_user_session_token(user) 51 | 52 | conn = 53 | conn 54 | |> put_session(:user_token, user_token) 55 | |> put_req_cookie(@remember_me_cookie, user_token) 56 | |> fetch_cookies() 57 | |> UserAuth.log_out_user() 58 | 59 | refute get_session(conn, :user_token) 60 | refute conn.cookies[@remember_me_cookie] 61 | assert %{max_age: 0} = conn.resp_cookies[@remember_me_cookie] 62 | assert redirected_to(conn) == "/" 63 | refute Accounts.get_user_by_session_token(user_token) 64 | end 65 | 66 | test "broadcasts to the given live_socket_id", %{conn: conn} do 67 | live_socket_id = "users_sessions:abcdef-token" 68 | SqliteScaleWeb.Endpoint.subscribe(live_socket_id) 69 | 70 | conn 71 | |> put_session(:live_socket_id, live_socket_id) 72 | |> UserAuth.log_out_user() 73 | 74 | assert_receive %Phoenix.Socket.Broadcast{event: "disconnect", topic: ^live_socket_id} 75 | end 76 | 77 | test "works even if user is already logged out", %{conn: conn} do 78 | conn = conn |> fetch_cookies() |> UserAuth.log_out_user() 79 | refute get_session(conn, :user_token) 80 | assert %{max_age: 0} = conn.resp_cookies[@remember_me_cookie] 81 | assert redirected_to(conn) == "/" 82 | end 83 | end 84 | 85 | describe "fetch_current_user/2" do 86 | test "authenticates user from session", %{conn: conn, user: user} do 87 | user_token = Accounts.generate_user_session_token(user) 88 | conn = conn |> put_session(:user_token, user_token) |> UserAuth.fetch_current_user([]) 89 | assert conn.assigns.current_user.id == user.id 90 | end 91 | 92 | test "authenticates user from cookies", %{conn: conn, user: user} do 93 | logged_in_conn = 94 | conn |> fetch_cookies() |> UserAuth.log_in_user(user, %{"remember_me" => "true"}) 95 | 96 | user_token = logged_in_conn.cookies[@remember_me_cookie] 97 | %{value: signed_token} = logged_in_conn.resp_cookies[@remember_me_cookie] 98 | 99 | conn = 100 | conn 101 | |> put_req_cookie(@remember_me_cookie, signed_token) 102 | |> UserAuth.fetch_current_user([]) 103 | 104 | assert get_session(conn, :user_token) == user_token 105 | assert conn.assigns.current_user.id == user.id 106 | end 107 | 108 | test "does not authenticate if data is missing", %{conn: conn, user: user} do 109 | _ = Accounts.generate_user_session_token(user) 110 | conn = UserAuth.fetch_current_user(conn, []) 111 | refute get_session(conn, :user_token) 112 | refute conn.assigns.current_user 113 | end 114 | end 115 | 116 | describe "redirect_if_user_is_authenticated/2" do 117 | test "redirects if user is authenticated", %{conn: conn, user: user} do 118 | conn = conn |> assign(:current_user, user) |> UserAuth.redirect_if_user_is_authenticated([]) 119 | assert conn.halted 120 | assert redirected_to(conn) == "/" 121 | end 122 | 123 | test "does not redirect if user is not authenticated", %{conn: conn} do 124 | conn = UserAuth.redirect_if_user_is_authenticated(conn, []) 125 | refute conn.halted 126 | refute conn.status 127 | end 128 | end 129 | 130 | describe "require_authenticated_user/2" do 131 | test "redirects if user is not authenticated", %{conn: conn} do 132 | conn = conn |> fetch_flash() |> UserAuth.require_authenticated_user([]) 133 | assert conn.halted 134 | assert redirected_to(conn) == Routes.user_session_path(conn, :new) 135 | assert get_flash(conn, :error) == "You must log in to access this page." 136 | end 137 | 138 | test "stores the path to redirect to on GET", %{conn: conn} do 139 | halted_conn = 140 | %{conn | path_info: ["foo"], query_string: ""} 141 | |> fetch_flash() 142 | |> UserAuth.require_authenticated_user([]) 143 | 144 | assert halted_conn.halted 145 | assert get_session(halted_conn, :user_return_to) == "/foo" 146 | 147 | halted_conn = 148 | %{conn | path_info: ["foo"], query_string: "bar=baz"} 149 | |> fetch_flash() 150 | |> UserAuth.require_authenticated_user([]) 151 | 152 | assert halted_conn.halted 153 | assert get_session(halted_conn, :user_return_to) == "/foo?bar=baz" 154 | 155 | halted_conn = 156 | %{conn | path_info: ["foo"], query_string: "bar", method: "POST"} 157 | |> fetch_flash() 158 | |> UserAuth.require_authenticated_user([]) 159 | 160 | assert halted_conn.halted 161 | refute get_session(halted_conn, :user_return_to) 162 | end 163 | 164 | test "does not redirect if user is authenticated", %{conn: conn, user: user} do 165 | conn = conn |> assign(:current_user, user) |> UserAuth.require_authenticated_user([]) 166 | refute conn.halted 167 | refute conn.status 168 | end 169 | end 170 | end 171 | -------------------------------------------------------------------------------- /test/sqlite_scale_web/controllers/user_confirmation_controller_test.exs: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.UserConfirmationControllerTest do 2 | use SqliteScaleWeb.ConnCase 3 | 4 | alias SqliteScale.Accounts 5 | alias SqliteScale.Repo 6 | import SqliteScale.AccountsFixtures 7 | 8 | setup do 9 | %{user: user_fixture()} 10 | end 11 | 12 | describe "GET /users/confirm" do 13 | test "renders the resend confirmation page", %{conn: conn} do 14 | conn = get(conn, Routes.user_confirmation_path(conn, :new)) 15 | response = html_response(conn, 200) 16 | assert response =~ "

Resend confirmation instructions

" 17 | end 18 | end 19 | 20 | describe "POST /users/confirm" do 21 | @tag :capture_log 22 | test "sends a new confirmation token", %{conn: conn, user: user} do 23 | conn = 24 | post(conn, Routes.user_confirmation_path(conn, :create), %{ 25 | "user" => %{"email" => user.email} 26 | }) 27 | 28 | assert redirected_to(conn) == "/" 29 | assert get_flash(conn, :info) =~ "If your email is in our system" 30 | assert Repo.get_by!(Accounts.UserToken, user_id: user.id).context == "confirm" 31 | end 32 | 33 | test "does not send confirmation token if User is confirmed", %{conn: conn, user: user} do 34 | Repo.update!(Accounts.User.confirm_changeset(user)) 35 | 36 | conn = 37 | post(conn, Routes.user_confirmation_path(conn, :create), %{ 38 | "user" => %{"email" => user.email} 39 | }) 40 | 41 | assert redirected_to(conn) == "/" 42 | assert get_flash(conn, :info) =~ "If your email is in our system" 43 | refute Repo.get_by(Accounts.UserToken, user_id: user.id) 44 | end 45 | 46 | test "does not send confirmation token if email is invalid", %{conn: conn} do 47 | conn = 48 | post(conn, Routes.user_confirmation_path(conn, :create), %{ 49 | "user" => %{"email" => "unknown@example.com"} 50 | }) 51 | 52 | assert redirected_to(conn) == "/" 53 | assert get_flash(conn, :info) =~ "If your email is in our system" 54 | assert Repo.all(Accounts.UserToken) == [] 55 | end 56 | end 57 | 58 | describe "GET /users/confirm/:token" do 59 | test "renders the confirmation page", %{conn: conn} do 60 | conn = get(conn, Routes.user_confirmation_path(conn, :edit, "some-token")) 61 | response = html_response(conn, 200) 62 | assert response =~ "

Confirm account

" 63 | 64 | form_action = Routes.user_confirmation_path(conn, :update, "some-token") 65 | assert response =~ "action=\"#{form_action}\"" 66 | end 67 | end 68 | 69 | describe "POST /users/confirm/:token" do 70 | test "confirms the given token once", %{conn: conn, user: user} do 71 | token = 72 | extract_user_token(fn url -> 73 | Accounts.deliver_user_confirmation_instructions(user, url) 74 | end) 75 | 76 | conn = post(conn, Routes.user_confirmation_path(conn, :update, token)) 77 | assert redirected_to(conn) == "/" 78 | assert get_flash(conn, :info) =~ "User confirmed successfully" 79 | assert Accounts.get_user!(user.id).confirmed_at 80 | refute get_session(conn, :user_token) 81 | assert Repo.all(Accounts.UserToken) == [] 82 | 83 | # When not logged in 84 | conn = post(conn, Routes.user_confirmation_path(conn, :update, token)) 85 | assert redirected_to(conn) == "/" 86 | assert get_flash(conn, :error) =~ "User confirmation link is invalid or it has expired" 87 | 88 | # When logged in 89 | conn = 90 | build_conn() 91 | |> log_in_user(user) 92 | |> post(Routes.user_confirmation_path(conn, :update, token)) 93 | 94 | assert redirected_to(conn) == "/" 95 | refute get_flash(conn, :error) 96 | end 97 | 98 | test "does not confirm email with invalid token", %{conn: conn, user: user} do 99 | conn = post(conn, Routes.user_confirmation_path(conn, :update, "oops")) 100 | assert redirected_to(conn) == "/" 101 | assert get_flash(conn, :error) =~ "User confirmation link is invalid or it has expired" 102 | refute Accounts.get_user!(user.id).confirmed_at 103 | end 104 | end 105 | end 106 | -------------------------------------------------------------------------------- /test/sqlite_scale_web/controllers/user_registration_controller_test.exs: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.UserRegistrationControllerTest do 2 | use SqliteScaleWeb.ConnCase 3 | 4 | import SqliteScale.AccountsFixtures 5 | 6 | describe "GET /users/register" do 7 | test "renders registration page", %{conn: conn} do 8 | conn = get(conn, Routes.user_registration_path(conn, :new)) 9 | response = html_response(conn, 200) 10 | assert response =~ "

Register

" 11 | assert response =~ "Log in" 12 | assert response =~ "Register" 13 | end 14 | 15 | test "redirects if already logged in", %{conn: conn} do 16 | conn = conn |> log_in_user(user_fixture()) |> get(Routes.user_registration_path(conn, :new)) 17 | assert redirected_to(conn) == "/" 18 | end 19 | end 20 | 21 | describe "POST /users/register" do 22 | @tag :capture_log 23 | test "creates account and logs the user in", %{conn: conn} do 24 | email = unique_user_email() 25 | 26 | conn = 27 | post(conn, Routes.user_registration_path(conn, :create), %{ 28 | "user" => valid_user_attributes(email: email) 29 | }) 30 | 31 | assert get_session(conn, :user_token) 32 | assert redirected_to(conn) == "/" 33 | 34 | # Now do a logged in request and assert on the menu 35 | conn = get(conn, "/") 36 | response = html_response(conn, 200) 37 | assert response =~ email 38 | assert response =~ "Settings" 39 | assert response =~ "Log out" 40 | end 41 | 42 | test "render errors for invalid data", %{conn: conn} do 43 | conn = 44 | post(conn, Routes.user_registration_path(conn, :create), %{ 45 | "user" => %{"email" => "with spaces", "password" => "too short"} 46 | }) 47 | 48 | response = html_response(conn, 200) 49 | assert response =~ "

Register

" 50 | assert response =~ "must have the @ sign and no spaces" 51 | assert response =~ "should be at least 12 character" 52 | end 53 | end 54 | end 55 | -------------------------------------------------------------------------------- /test/sqlite_scale_web/controllers/user_reset_password_controller_test.exs: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.UserResetPasswordControllerTest do 2 | use SqliteScaleWeb.ConnCase 3 | 4 | alias SqliteScale.Accounts 5 | alias SqliteScale.Repo 6 | import SqliteScale.AccountsFixtures 7 | 8 | setup do 9 | %{user: user_fixture()} 10 | end 11 | 12 | describe "GET /users/reset_password" do 13 | test "renders the reset password page", %{conn: conn} do 14 | conn = get(conn, Routes.user_reset_password_path(conn, :new)) 15 | response = html_response(conn, 200) 16 | assert response =~ "

Forgot your password?

" 17 | end 18 | end 19 | 20 | describe "POST /users/reset_password" do 21 | @tag :capture_log 22 | test "sends a new reset password token", %{conn: conn, user: user} do 23 | conn = 24 | post(conn, Routes.user_reset_password_path(conn, :create), %{ 25 | "user" => %{"email" => user.email} 26 | }) 27 | 28 | assert redirected_to(conn) == "/" 29 | assert get_flash(conn, :info) =~ "If your email is in our system" 30 | assert Repo.get_by!(Accounts.UserToken, user_id: user.id).context == "reset_password" 31 | end 32 | 33 | test "does not send reset password token if email is invalid", %{conn: conn} do 34 | conn = 35 | post(conn, Routes.user_reset_password_path(conn, :create), %{ 36 | "user" => %{"email" => "unknown@example.com"} 37 | }) 38 | 39 | assert redirected_to(conn) == "/" 40 | assert get_flash(conn, :info) =~ "If your email is in our system" 41 | assert Repo.all(Accounts.UserToken) == [] 42 | end 43 | end 44 | 45 | describe "GET /users/reset_password/:token" do 46 | setup %{user: user} do 47 | token = 48 | extract_user_token(fn url -> 49 | Accounts.deliver_user_reset_password_instructions(user, url) 50 | end) 51 | 52 | %{token: token} 53 | end 54 | 55 | test "renders reset password", %{conn: conn, token: token} do 56 | conn = get(conn, Routes.user_reset_password_path(conn, :edit, token)) 57 | assert html_response(conn, 200) =~ "

Reset password

" 58 | end 59 | 60 | test "does not render reset password with invalid token", %{conn: conn} do 61 | conn = get(conn, Routes.user_reset_password_path(conn, :edit, "oops")) 62 | assert redirected_to(conn) == "/" 63 | assert get_flash(conn, :error) =~ "Reset password link is invalid or it has expired" 64 | end 65 | end 66 | 67 | describe "PUT /users/reset_password/:token" do 68 | setup %{user: user} do 69 | token = 70 | extract_user_token(fn url -> 71 | Accounts.deliver_user_reset_password_instructions(user, url) 72 | end) 73 | 74 | %{token: token} 75 | end 76 | 77 | test "resets password once", %{conn: conn, user: user, token: token} do 78 | conn = 79 | put(conn, Routes.user_reset_password_path(conn, :update, token), %{ 80 | "user" => %{ 81 | "password" => "new valid password", 82 | "password_confirmation" => "new valid password" 83 | } 84 | }) 85 | 86 | assert redirected_to(conn) == Routes.user_session_path(conn, :new) 87 | refute get_session(conn, :user_token) 88 | assert get_flash(conn, :info) =~ "Password reset successfully" 89 | assert Accounts.get_user_by_email_and_password(user.email, "new valid password") 90 | end 91 | 92 | test "does not reset password on invalid data", %{conn: conn, token: token} do 93 | conn = 94 | put(conn, Routes.user_reset_password_path(conn, :update, token), %{ 95 | "user" => %{ 96 | "password" => "too short", 97 | "password_confirmation" => "does not match" 98 | } 99 | }) 100 | 101 | response = html_response(conn, 200) 102 | assert response =~ "

Reset password

" 103 | assert response =~ "should be at least 12 character(s)" 104 | assert response =~ "does not match password" 105 | end 106 | 107 | test "does not reset password with invalid token", %{conn: conn} do 108 | conn = put(conn, Routes.user_reset_password_path(conn, :update, "oops")) 109 | assert redirected_to(conn) == "/" 110 | assert get_flash(conn, :error) =~ "Reset password link is invalid or it has expired" 111 | end 112 | end 113 | end 114 | -------------------------------------------------------------------------------- /test/sqlite_scale_web/controllers/user_session_controller_test.exs: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.UserSessionControllerTest do 2 | use SqliteScaleWeb.ConnCase 3 | 4 | import SqliteScale.AccountsFixtures 5 | 6 | setup do 7 | %{user: user_fixture()} 8 | end 9 | 10 | describe "GET /users/log_in" do 11 | test "renders log in page", %{conn: conn} do 12 | conn = get(conn, Routes.user_session_path(conn, :new)) 13 | response = html_response(conn, 200) 14 | assert response =~ "

Log in

" 15 | assert response =~ "Register" 16 | assert response =~ "Forgot your password?" 17 | end 18 | 19 | test "redirects if already logged in", %{conn: conn, user: user} do 20 | conn = conn |> log_in_user(user) |> get(Routes.user_session_path(conn, :new)) 21 | assert redirected_to(conn) == "/" 22 | end 23 | end 24 | 25 | describe "POST /users/log_in" do 26 | test "logs the user in", %{conn: conn, user: user} do 27 | conn = 28 | post(conn, Routes.user_session_path(conn, :create), %{ 29 | "user" => %{"email" => user.email, "password" => valid_user_password()} 30 | }) 31 | 32 | assert get_session(conn, :user_token) 33 | assert redirected_to(conn) == "/" 34 | 35 | # Now do a logged in request and assert on the menu 36 | conn = get(conn, "/") 37 | response = html_response(conn, 200) 38 | assert response =~ user.email 39 | assert response =~ "Settings" 40 | assert response =~ "Log out" 41 | end 42 | 43 | test "logs the user in with remember me", %{conn: conn, user: user} do 44 | conn = 45 | post(conn, Routes.user_session_path(conn, :create), %{ 46 | "user" => %{ 47 | "email" => user.email, 48 | "password" => valid_user_password(), 49 | "remember_me" => "true" 50 | } 51 | }) 52 | 53 | assert conn.resp_cookies["_sqlite_scale_web_user_remember_me"] 54 | assert redirected_to(conn) == "/" 55 | end 56 | 57 | test "logs the user in with return to", %{conn: conn, user: user} do 58 | conn = 59 | conn 60 | |> init_test_session(user_return_to: "/foo/bar") 61 | |> post(Routes.user_session_path(conn, :create), %{ 62 | "user" => %{ 63 | "email" => user.email, 64 | "password" => valid_user_password() 65 | } 66 | }) 67 | 68 | assert redirected_to(conn) == "/foo/bar" 69 | end 70 | 71 | test "emits error message with invalid credentials", %{conn: conn, user: user} do 72 | conn = 73 | post(conn, Routes.user_session_path(conn, :create), %{ 74 | "user" => %{"email" => user.email, "password" => "invalid_password"} 75 | }) 76 | 77 | response = html_response(conn, 200) 78 | assert response =~ "

Log in

" 79 | assert response =~ "Invalid email or password" 80 | end 81 | end 82 | 83 | describe "DELETE /users/log_out" do 84 | test "logs the user out", %{conn: conn, user: user} do 85 | conn = conn |> log_in_user(user) |> delete(Routes.user_session_path(conn, :delete)) 86 | assert redirected_to(conn) == "/" 87 | refute get_session(conn, :user_token) 88 | assert get_flash(conn, :info) =~ "Logged out successfully" 89 | end 90 | 91 | test "succeeds even if the user is not logged in", %{conn: conn} do 92 | conn = delete(conn, Routes.user_session_path(conn, :delete)) 93 | assert redirected_to(conn) == "/" 94 | refute get_session(conn, :user_token) 95 | assert get_flash(conn, :info) =~ "Logged out successfully" 96 | end 97 | end 98 | end 99 | -------------------------------------------------------------------------------- /test/sqlite_scale_web/controllers/user_settings_controller_test.exs: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.UserSettingsControllerTest do 2 | use SqliteScaleWeb.ConnCase 3 | 4 | alias SqliteScale.Accounts 5 | import SqliteScale.AccountsFixtures 6 | 7 | setup :register_and_log_in_user 8 | 9 | describe "GET /users/settings" do 10 | test "renders settings page", %{conn: conn} do 11 | conn = get(conn, Routes.user_settings_path(conn, :edit)) 12 | response = html_response(conn, 200) 13 | assert response =~ "

Settings

" 14 | end 15 | 16 | test "redirects if user is not logged in" do 17 | conn = build_conn() 18 | conn = get(conn, Routes.user_settings_path(conn, :edit)) 19 | assert redirected_to(conn) == Routes.user_session_path(conn, :new) 20 | end 21 | end 22 | 23 | describe "PUT /users/settings (change password form)" do 24 | test "updates the user password and resets tokens", %{conn: conn, user: user} do 25 | new_password_conn = 26 | put(conn, Routes.user_settings_path(conn, :update), %{ 27 | "action" => "update_password", 28 | "current_password" => valid_user_password(), 29 | "user" => %{ 30 | "password" => "new valid password", 31 | "password_confirmation" => "new valid password" 32 | } 33 | }) 34 | 35 | assert redirected_to(new_password_conn) == Routes.user_settings_path(conn, :edit) 36 | assert get_session(new_password_conn, :user_token) != get_session(conn, :user_token) 37 | assert get_flash(new_password_conn, :info) =~ "Password updated successfully" 38 | assert Accounts.get_user_by_email_and_password(user.email, "new valid password") 39 | end 40 | 41 | test "does not update password on invalid data", %{conn: conn} do 42 | old_password_conn = 43 | put(conn, Routes.user_settings_path(conn, :update), %{ 44 | "action" => "update_password", 45 | "current_password" => "invalid", 46 | "user" => %{ 47 | "password" => "too short", 48 | "password_confirmation" => "does not match" 49 | } 50 | }) 51 | 52 | response = html_response(old_password_conn, 200) 53 | assert response =~ "

Settings

" 54 | assert response =~ "should be at least 12 character(s)" 55 | assert response =~ "does not match password" 56 | assert response =~ "is not valid" 57 | 58 | assert get_session(old_password_conn, :user_token) == get_session(conn, :user_token) 59 | end 60 | end 61 | 62 | describe "PUT /users/settings (change email form)" do 63 | @tag :capture_log 64 | test "updates the user email", %{conn: conn, user: user} do 65 | conn = 66 | put(conn, Routes.user_settings_path(conn, :update), %{ 67 | "action" => "update_email", 68 | "current_password" => valid_user_password(), 69 | "user" => %{"email" => unique_user_email()} 70 | }) 71 | 72 | assert redirected_to(conn) == Routes.user_settings_path(conn, :edit) 73 | assert get_flash(conn, :info) =~ "A link to confirm your email" 74 | assert Accounts.get_user_by_email(user.email) 75 | end 76 | 77 | test "does not update email on invalid data", %{conn: conn} do 78 | conn = 79 | put(conn, Routes.user_settings_path(conn, :update), %{ 80 | "action" => "update_email", 81 | "current_password" => "invalid", 82 | "user" => %{"email" => "with spaces"} 83 | }) 84 | 85 | response = html_response(conn, 200) 86 | assert response =~ "

Settings

" 87 | assert response =~ "must have the @ sign and no spaces" 88 | assert response =~ "is not valid" 89 | end 90 | end 91 | 92 | describe "GET /users/settings/confirm_email/:token" do 93 | setup %{user: user} do 94 | email = unique_user_email() 95 | 96 | token = 97 | extract_user_token(fn url -> 98 | Accounts.deliver_update_email_instructions(%{user | email: email}, user.email, url) 99 | end) 100 | 101 | %{token: token, email: email} 102 | end 103 | 104 | test "updates the user email once", %{conn: conn, user: user, token: token, email: email} do 105 | conn = get(conn, Routes.user_settings_path(conn, :confirm_email, token)) 106 | assert redirected_to(conn) == Routes.user_settings_path(conn, :edit) 107 | assert get_flash(conn, :info) =~ "Email changed successfully" 108 | refute Accounts.get_user_by_email(user.email) 109 | assert Accounts.get_user_by_email(email) 110 | 111 | conn = get(conn, Routes.user_settings_path(conn, :confirm_email, token)) 112 | assert redirected_to(conn) == Routes.user_settings_path(conn, :edit) 113 | assert get_flash(conn, :error) =~ "Email change link is invalid or it has expired" 114 | end 115 | 116 | test "does not update email with invalid token", %{conn: conn, user: user} do 117 | conn = get(conn, Routes.user_settings_path(conn, :confirm_email, "oops")) 118 | assert redirected_to(conn) == Routes.user_settings_path(conn, :edit) 119 | assert get_flash(conn, :error) =~ "Email change link is invalid or it has expired" 120 | assert Accounts.get_user_by_email(user.email) 121 | end 122 | 123 | test "redirects if user is not logged in", %{token: token} do 124 | conn = build_conn() 125 | conn = get(conn, Routes.user_settings_path(conn, :confirm_email, token)) 126 | assert redirected_to(conn) == Routes.user_session_path(conn, :new) 127 | end 128 | end 129 | end 130 | -------------------------------------------------------------------------------- /test/sqlite_scale_web/views/error_view_test.exs: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.ErrorViewTest do 2 | use SqliteScaleWeb.ConnCase, async: true 3 | 4 | # Bring render/3 and render_to_string/3 for testing custom views 5 | import Phoenix.View 6 | 7 | test "renders 404.html" do 8 | assert render_to_string(SqliteScaleWeb.ErrorView, "404.html", []) == "Not Found" 9 | end 10 | 11 | test "renders 500.html" do 12 | assert render_to_string(SqliteScaleWeb.ErrorView, "500.html", []) == "Internal Server Error" 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /test/sqlite_scale_web/views/layout_view_test.exs: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.LayoutViewTest do 2 | use SqliteScaleWeb.ConnCase, async: true 3 | 4 | # When testing helpers, you may want to import Phoenix.HTML and 5 | # use functions such as safe_to_string() to convert the helper 6 | # result into an HTML string. 7 | # import Phoenix.HTML 8 | end 9 | -------------------------------------------------------------------------------- /test/sqlite_scale_web/views/page_view_test.exs: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.PageViewTest do 2 | use SqliteScaleWeb.ConnCase, async: true 3 | end 4 | -------------------------------------------------------------------------------- /test/support/channel_case.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.ChannelCase do 2 | @moduledoc """ 3 | This module defines the test case to be used by 4 | channel tests. 5 | 6 | Such tests rely on `Phoenix.ChannelTest` and also 7 | import other functionality to make it easier 8 | to build common data structures and query the data layer. 9 | 10 | Finally, if the test case interacts with the database, 11 | we enable the SQL sandbox, so changes done to the database 12 | are reverted at the end of every test. If you are using 13 | PostgreSQL, you can even run database tests asynchronously 14 | by setting `use SqliteScaleWeb.ChannelCase, async: true`, although 15 | this option is not recommended for other databases. 16 | """ 17 | 18 | use ExUnit.CaseTemplate 19 | 20 | using do 21 | quote do 22 | # Import conveniences for testing with channels 23 | import Phoenix.ChannelTest 24 | import SqliteScaleWeb.ChannelCase 25 | 26 | # The default endpoint for testing 27 | @endpoint SqliteScaleWeb.Endpoint 28 | end 29 | end 30 | 31 | setup tags do 32 | pid = Ecto.Adapters.SQL.Sandbox.start_owner!(SqliteScale.Repo, shared: not tags[:async]) 33 | on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end) 34 | :ok 35 | end 36 | end 37 | -------------------------------------------------------------------------------- /test/support/conn_case.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScaleWeb.ConnCase do 2 | @moduledoc """ 3 | This module defines the test case to be used by 4 | tests that require setting up a connection. 5 | 6 | Such tests rely on `Phoenix.ConnTest` and also 7 | import other functionality to make it easier 8 | to build common data structures and query the data layer. 9 | 10 | Finally, if the test case interacts with the database, 11 | we enable the SQL sandbox, so changes done to the database 12 | are reverted at the end of every test. If you are using 13 | PostgreSQL, you can even run database tests asynchronously 14 | by setting `use SqliteScaleWeb.ConnCase, async: true`, although 15 | this option is not recommended for other databases. 16 | """ 17 | 18 | use ExUnit.CaseTemplate 19 | 20 | using do 21 | quote do 22 | # Import conveniences for testing with connections 23 | import Plug.Conn 24 | import Phoenix.ConnTest 25 | import SqliteScaleWeb.ConnCase 26 | 27 | alias SqliteScaleWeb.Router.Helpers, as: Routes 28 | 29 | # The default endpoint for testing 30 | @endpoint SqliteScaleWeb.Endpoint 31 | end 32 | end 33 | 34 | setup tags do 35 | pid = Ecto.Adapters.SQL.Sandbox.start_owner!(SqliteScale.Repo, shared: not tags[:async]) 36 | on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end) 37 | {:ok, conn: Phoenix.ConnTest.build_conn()} 38 | end 39 | 40 | @doc """ 41 | Setup helper that registers and logs in users. 42 | 43 | setup :register_and_log_in_user 44 | 45 | It stores an updated connection and a registered user in the 46 | test context. 47 | """ 48 | def register_and_log_in_user(%{conn: conn}) do 49 | user = SqliteScale.AccountsFixtures.user_fixture() 50 | %{conn: log_in_user(conn, user), user: user} 51 | end 52 | 53 | @doc """ 54 | Logs the given `user` into the `conn`. 55 | 56 | It returns an updated `conn`. 57 | """ 58 | def log_in_user(conn, user) do 59 | token = SqliteScale.Accounts.generate_user_session_token(user) 60 | 61 | conn 62 | |> Phoenix.ConnTest.init_test_session(%{}) 63 | |> Plug.Conn.put_session(:user_token, token) 64 | end 65 | end 66 | -------------------------------------------------------------------------------- /test/support/data_case.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.DataCase do 2 | @moduledoc """ 3 | This module defines the setup for tests requiring 4 | access to the application's data layer. 5 | 6 | You may define functions here to be used as helpers in 7 | your tests. 8 | 9 | Finally, if the test case interacts with the database, 10 | we enable the SQL sandbox, so changes done to the database 11 | are reverted at the end of every test. If you are using 12 | PostgreSQL, you can even run database tests asynchronously 13 | by setting `use SqliteScale.DataCase, async: true`, although 14 | this option is not recommended for other databases. 15 | """ 16 | 17 | use ExUnit.CaseTemplate 18 | 19 | using do 20 | quote do 21 | alias SqliteScale.Repo 22 | 23 | import Ecto 24 | import Ecto.Changeset 25 | import Ecto.Query 26 | import SqliteScale.DataCase 27 | end 28 | end 29 | 30 | setup tags do 31 | pid = Ecto.Adapters.SQL.Sandbox.start_owner!(SqliteScale.Repo, shared: not tags[:async]) 32 | on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end) 33 | :ok 34 | end 35 | 36 | @doc """ 37 | A helper that transforms changeset errors into a map of messages. 38 | 39 | assert {:error, changeset} = Accounts.create_user(%{password: "short"}) 40 | assert "password is too short" in errors_on(changeset).password 41 | assert %{password: ["password is too short"]} = errors_on(changeset) 42 | 43 | """ 44 | def errors_on(changeset) do 45 | Ecto.Changeset.traverse_errors(changeset, fn {message, opts} -> 46 | Regex.replace(~r"%{(\w+)}", message, fn _, key -> 47 | opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string() 48 | end) 49 | end) 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /test/support/fixtures/accounts_fixtures.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.AccountsFixtures do 2 | @moduledoc """ 3 | This module defines test helpers for creating 4 | entities via the `SqliteScale.Accounts` context. 5 | """ 6 | 7 | def unique_user_email, do: "user#{System.unique_integer()}@example.com" 8 | def valid_user_password, do: "hello world!" 9 | 10 | def valid_user_attributes(attrs \\ %{}) do 11 | Enum.into(attrs, %{ 12 | email: unique_user_email(), 13 | password: valid_user_password() 14 | }) 15 | end 16 | 17 | def user_fixture(attrs \\ %{}) do 18 | {:ok, user} = 19 | attrs 20 | |> valid_user_attributes() 21 | |> SqliteScale.Accounts.register_user() 22 | 23 | user 24 | end 25 | 26 | def extract_user_token(fun) do 27 | {:ok, captured_email} = fun.(&"[TOKEN]#{&1}[TOKEN]") 28 | [_, token | _] = String.split(captured_email.text_body, "[TOKEN]") 29 | token 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /test/support/fixtures/todo_items_fixtures.ex: -------------------------------------------------------------------------------- 1 | defmodule SqliteScale.TodoItemsFixtures do 2 | @moduledoc """ 3 | This module defines test helpers for creating 4 | entities via the `SqliteScale.TodoItems` context. 5 | """ 6 | 7 | @doc """ 8 | Generate a todo_item. 9 | """ 10 | def todo_item_fixture(attrs \\ %{}) do 11 | {:ok, todo_item} = 12 | attrs 13 | |> Enum.into(%{ 14 | item: "some item" 15 | }) 16 | |> SqliteScale.TodoItems.create_todo_item() 17 | 18 | todo_item 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start() 2 | Ecto.Adapters.SQL.Sandbox.mode(SqliteScale.Repo, :manual) 3 | --------------------------------------------------------------------------------