-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
74 auth via email link gem passwordless (#75)
* add passwordless * add gem letter-opener * add passwordless views * make sign in work * users#show, basic views work * Update new.html.erb * no more need to load insta user in session * insta user belongs to user * add passwordless initializer * prepare for sending emails in production * require user for users controller, i18n * Update new.html.erb * associate insta user with user * display insta users of a user * no need for user partial in insta users index * validation for import action * move import action from ig_posts to ig_user * Update show.html.erb * Update show.html.erb * fix import * depreciate insta_user#show in favour of insta posts#index * Update new.html.erb * after connect insta account redirect to user path * Update users_controller.rb * annotate routes * update tests * update AWS SES creds for prod * Update user.rb
- Loading branch information
Showing
39 changed files
with
370 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
class ApplicationController < ActionController::Base | ||
include Passwordless::ControllerHelpers | ||
|
||
helper_method :current_user | ||
|
||
private | ||
|
||
def current_user | ||
@current_user ||= InstaUser.find(session[:insta_user_id]) if session[:insta_user_id] | ||
rescue ActiveRecord::RecordNotFound | ||
nil | ||
@current_user ||= authenticate_by_session(User) | ||
end | ||
|
||
def require_user! | ||
return if current_user | ||
|
||
redirect_to root_path, flash: { alert: t('notifications.unauthorized') } | ||
end | ||
helper_method :current_user | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,15 +19,18 @@ def callback | |
insta_user_id = InstaAuthService.new(code, redirect_uri).call | ||
return head :bad_request unless insta_user_id | ||
|
||
session[:insta_user_id] = insta_user_id | ||
insta_user = InstaUser.find(insta_user_id) | ||
insta_user.update(user: current_user) | ||
|
||
redirect_to insta_user_path(insta_user_id) | ||
redirect_to user_path | ||
end | ||
|
||
# GET /instagram/delete | ||
def delete | ||
render plain: 'Please contact [email protected] to delete your data' | ||
end | ||
|
||
# GET /instagram/deauthorize | ||
def deauthorize | ||
render plain: 'Please contact [email protected] to deauthorize the app' | ||
end | ||
|
@@ -36,10 +39,8 @@ def deauthorize | |
|
||
def redirect_uri | ||
if Rails.env.production? | ||
# Rails.application.routes.url_helpers.instagram_callback_url | ||
instagram_callback_url | ||
else | ||
# staging | ||
# 'localhost:3000/instagram/callback/' | ||
# 'https://insta2blog.com/instagram/callback' | ||
'https://insta2site.herokuapp.com/' | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class UsersController < ApplicationController | ||
before_action :require_user! | ||
|
||
# GET /me | ||
def show | ||
@user = current_user | ||
@insta_users = @user.insta_users.order(created_at: :desc) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
class ApplicationMailer < ActionMailer::Base | ||
default from: 'from@example.com' | ||
default from: 'hello@insta2blog.com' | ||
layout 'mailer' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class User < ApplicationRecord | ||
has_many :insta_users, dependent: :destroy | ||
|
||
validates :email, | ||
presence: true, | ||
uniqueness: { case_sensitive: false }, | ||
format: { with: URI::MailTo::EMAIL_REGEXP } | ||
|
||
passwordless_with :email | ||
|
||
def self.fetch_resource_for_passwordless(email) | ||
find_or_create_by(email:) | ||
end | ||
|
||
def username | ||
email.split('@').first | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,42 @@ | ||
<%= link_to insta_user.at_username, insta_user_posts_path(insta_user), class: 'bg-slate-50 border border-slate-300 rounded-md p-4 lg:w-1/3 md:w-2/3 sm:w-full hover:bg-slate-100' %> | ||
<article class='bg-slate-50 border border-slate-300 max-w-md rounded-md'> | ||
<div class='p-2 border-b border-y-slate-300 text-center'> | ||
<h4 class='text-lg text-gray-700 font-semibold'> | ||
<%= link_to insta_user.at_username, insta_user_posts_path(insta_user), class: 'hover:text-violet-700' %> | ||
</h4> | ||
</div> | ||
|
||
<div class='p-2 border-b border-y-slate-300'> | ||
<div class='text-center space-y-2'> | ||
<div class='text-sm'> | ||
<b><%= insta_user.media_count %></b> | ||
posts detected | ||
</div> | ||
|
||
<div class='text-sm'> | ||
<b><%= insta_user.insta_posts_count %></b> | ||
posts imported | ||
</div> | ||
|
||
<%= button_to import_insta_user_path(insta_user), class: 'bg-violet-50 border border-dotted border-violet-300 rounded-md p-2 hover:bg-violet-100 text-violet-700' do %> | ||
<i class="fa-solid fa-cloud-arrow-down text-violet-700"></i> | ||
<span>Import posts</span> | ||
<% end %> | ||
<% if insta_user.last_import_at.present? %> | ||
<span class='text-xs text-slate-500'> | ||
Last import: | ||
<%= insta_user.last_import_at.to_fs(:long) %> | ||
</span> | ||
<% end %> | ||
</div> | ||
</div> | ||
|
||
<div class='p-2'> | ||
<div> | ||
<%= link_to 'Disconnect', '/instagram/deauthorize', class: 'text-blue-600 hover:text-red-600', target: '_blank', rel: 'noopener' %> | ||
</div> | ||
<div> | ||
<%= link_to 'Delete all data', '/instagram/delete', class: 'text-blue-600 hover:text-red-600', target: '_blank', rel: 'noopener' %> | ||
</div> | ||
</div> | ||
</article> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= I18n.t('passwordless.mailer.magic_link', link: @magic_link) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p><%= I18n.t('passwordless.sessions.create.email_sent_if_record_found') %></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<div class='text-center space-y-4'> | ||
<h1 class='font-semibold text-4xl'> | ||
<i class="fa-solid fa-wand-magic-sparkles"></i> | ||
<i class="fa-regular fa-envelope-open"></i> | ||
</h1> | ||
<h4 class='font-semibold text-lg'> | ||
Enter your email to continue. | ||
</h4> | ||
<h4 class='font-semibold text-md'> | ||
You will receive a magic link to log in. | ||
</h4> | ||
</div> | ||
|
||
<%= form_with model: @session, url: send(Passwordless.mounted_as).sign_in_path, data: { turbo: 'false' }, class: 'text-center space-y-4' do |f| %> | ||
<div> | ||
<% email_field_name = :"passwordless[#{@email_field}]" %> | ||
<%= text_field_tag email_field_name, params.fetch(email_field_name, nil), required: true, class: 'rounded-md w-full' %> | ||
</div> | ||
<div> | ||
<%= f.submit I18n.t('passwordless.sessions.new.submit'), class: 'cursor-pointer w-full bg-violet-700 hover:bg-violet-900 text-fuchsia-50 p-2 rounded-md font-bold' %> | ||
</div> | ||
<% end %> |
Oops, something went wrong.