You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+139-1Lines changed: 139 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,12 @@
2
2
3
3
An authentication generator for Rails 7. Based on the [step-by-step guide on how to build your own authentication system in Rails from scratch](https://github.com/stevepolitodesign/rails-authentication-from-scratch).
- Requires a user to confirm their email address before they can log in.
55
+
- Allows a user to remain logged into the application even if they exit their browser.
56
+
- Allows a user to have multiple sessions. This gives users the ability to log out of all sessions at once. This also makes it easy to detect suspicious login activity.
57
+
- Allows a user to change their email address.
58
+
- Allows a user to recover their account if they forget their password.
59
+
- Requires users to submit their password anytime they're chaning their account information.
60
+
61
+
## 🔨 Usage
62
+
63
+
The following methods are automatically included in the corresponding generated files.
64
+
65
+
### Controller Methods
66
+
67
+
#### authenticate_user!
68
+
69
+
Redirects the visitor to the `login_path` if they're not logged in. Useful for preventing an anonymous user from accessing a page intended for an authenticated user.
70
+
71
+
#### current_user
72
+
73
+
Returns an instance of `User` if there's one in the session. Othwerwise returns `nil`.
74
+
75
+
#### forget_active_session
76
+
77
+
Deletes the `:remember_token` cookie. For added security, the associated `active_session` should be deleted too.
78
+
79
+
#### login(user)
80
+
81
+
[Resets](https://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-reset_session) the session and then creates a new `active_session` with on the `user` that was passed in. Stores the `id` of the `active_session` in the `session`. Returns the new `active_session`.
82
+
83
+
#### logout
84
+
85
+
[Resets](https://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-reset_session) the session and deletes the associated `active_session` record.
86
+
87
+
#### user_signed_in?
88
+
89
+
Returns `true` if `current_user` does not return `nil`. Othwerwise returns `false`.
90
+
91
+
#### redirect_if_authenticated
92
+
93
+
Redirects the user to the `root_path` if the user is logged in. Useful for keeping a user from accessing a page intended for an anonymous user.
94
+
95
+
#### remember(active_session)
96
+
97
+
Creates a cookie to store the value of the `remember_token` from the `active_session` that was passed in.
98
+
99
+
### View Helpers
100
+
101
+
#### current_user
102
+
103
+
Returns an instance of `User` if there's one in the session. Othwerwise returns `nil`.
104
+
105
+
#### user_signed_in?
106
+
107
+
Returns `true` if `current_user` does not return `nil`. Othwerwise returns `false`.
108
+
109
+
### User Model
110
+
111
+
#### self.authenticate_by(attributes)
112
+
113
+
A copy of the [authenticate_by](https://edgeapi.rubyonrails.org/classes/ActiveRecord/SecurePassword/ClassMethods.html#method-i-authenticate_by) class method that is set to ship in rails 7.1
114
+
115
+
#### confirm!
116
+
117
+
Sets the `confirmed_at` column to `Time.current`. Updates the `email` column if reconfirming a new email address. Returns `true` or `false`.
118
+
119
+
#### confirmed?
120
+
121
+
Returns `true` or `false` based on if the `confirmed_at` column is present.
122
+
123
+
#### confirmable_email
124
+
125
+
Returns the value of the `email` column if the `unconfirmed_email` column is empty. Otherwise, the value of `unconfirmed_email` is returned.
126
+
127
+
#### generate_confirmation_token
128
+
129
+
Generates a [signed_id](https://api.rubyonrails.org/classes/ActiveRecord/SignedId.html#method-i-signed_id) used in the confirmation mailer.
130
+
131
+
#### generate_password_reset_token
132
+
133
+
Generates a [signed_id](https://api.rubyonrails.org/classes/ActiveRecord/SignedId.html#method-i-signed_id) used in the password reset mailer.
134
+
135
+
#### send_confirmation_email!
136
+
137
+
Send a confirmation email to the user.
138
+
139
+
#### send_password_reset_email!
140
+
141
+
Send a password reset email to the user.
142
+
143
+
#### reconfirming?
144
+
145
+
Returns `true` if there's a value for `unconfirmed_email`. Otherwise `false` is returned.
146
+
147
+
#### unconfirmed?
148
+
149
+
Returns `true` if there's no value for `confirmed_at`. Otherwise `false` is returned.
150
+
151
+
#### unconfirmed_or_reconfirming?
152
+
153
+
Returns `true` if the user is unconfirmed or reconfirming a new email address. Otherwise `false` is returned.
154
+
155
+
### Test Helpers
156
+
157
+
#### current_user
158
+
159
+
Returns an instance of `User` if there's one in the test session. Othwerwise returns `nil`.
160
+
161
+
#### login(user, remember_user: nil)
162
+
163
+
Creates a `post` request to the `login_path`. Simulates a real login.
164
+
165
+
#### logout
166
+
167
+
Deletes the `current_active_session_id` test session. Simulates a login.
168
+
169
+
## ⚖️ Benefits
170
+
171
+
What makes this gem _different_ (not better) from [devise](https://github.com/heartcombo/devise), [clearance](https://github.com/thoughtbot/clearance/), etc?
172
+
173
+
1. This gem is less of an [engine](https://guides.rubyonrails.org/engines.html) and more of a [generator](https://guides.rubyonrails.org/generators.html). It generates all necessary models, views, controllers, mailers, and migrations. This means you have complete control over your authentication system and don't have to worry about learning a new DSL or API.
174
+
2. It also generates tests. That way you can ship with confidence if and when you decide to change how your authentication system works.
175
+
3. It utilizes modern core features of Rails, such as [ActiveSupport::CurrentAttributes](https://api.rubyonrails.org/classes/ActiveSupport/CurrentAttributes.html) and [Active Record Signed Id](https://api.rubyonrails.org/classes/ActiveRecord/SignedId.html#method-i-signed_id), [has_secure_password](https://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html#method-i-has_secure_password) and [has_secure_token](https://api.rubyonrails.org/classes/ActiveRecord/SecureToken/ClassMethods.html#method-i-has_secure_token).
176
+
4. It stores the session in the database. This gives users the ability to log out of all sessions at once. This also makes it easy to detect suspicious login activity.
177
+
40
178
## 🙏 Contributing
41
179
42
180
If you'd like to open a PR please make sure the following things pass:
@@ -47,4 +185,4 @@ bundle exec standardrb
47
185
```
48
186
## 📜 License
49
187
50
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
188
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
0 commit comments