forked from zauberware/rails-devise-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add devise invitable and a invite and accept invite mutation
- Loading branch information
1 parent
cbc37ae
commit 21d0e48
Showing
15 changed files
with
298 additions
and
34 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
module Auth | ||
# Custom passwords controller | ||
class InvitationsController < Devise::InvitationsController | ||
|
||
# GET /resource/invitation/accept?invitation_token=abcdef | ||
# redirect user to front end to finish invitation | ||
def edit | ||
redirect_to "http://#{ENV['CLIENT_URL']}/users/invitation/accept?invitation_token=#{params[:invitation_token]}" | ||
end | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mutations | ||
module Users | ||
# Accepts an invitation for a user | ||
class AcceptInvite < Mutations::BaseMutation | ||
description 'Accepts an invitation for a user' | ||
argument :invitation_token, String, required: true | ||
argument :attributes, Types::Users::UserInputType, required: true | ||
payload_type Boolean | ||
|
||
def resolve(invitation_token:, attributes:) | ||
user = User.accept_invitation!(attributes.to_h.merge(invitation_token: invitation_token)) | ||
raise ActiveRecord::RecordInvalid, user unless user.errors.empty? | ||
|
||
true | ||
end | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mutations | ||
module Users | ||
# Invites an user to your account. | ||
class InviteUser < Mutations::BaseMutation | ||
description 'Invites an user to your account.' | ||
argument :attributes, Types::Users::UserInputType, required: true | ||
payload_type Types::Users::UserType | ||
|
||
def resolve(attributes:) | ||
# create a dummy user object to check ability against create | ||
user = ::User.new(attributes.to_h.merge(company_id: context[:current_user].company_id)) | ||
current_ability.authorize! :create, user | ||
|
||
user = User.invite!(user.attributes, context[:current_user]) | ||
raise ActiveRecord::RecordInvalid, user unless user.errors.empty? | ||
|
||
user | ||
end | ||
end | ||
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
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
en: | ||
devise: | ||
failure: | ||
invited: "You have a pending invitation, accept it to finish creating your account." | ||
invitations: | ||
send_instructions: "An invitation email has been sent to %{email}." | ||
invitation_token_invalid: "The invitation token provided is not valid!" | ||
updated: "Your password was set successfully. You are now signed in." | ||
updated_not_active: "Your password was set successfully." | ||
no_invitations_remaining: "No invitations remaining" | ||
invitation_removed: "Your invitation was removed." | ||
new: | ||
header: "Send invitation" | ||
submit_button: "Send an invitation" | ||
edit: | ||
header: "Set your password" | ||
submit_button: "Set my password" | ||
mailer: | ||
invitation_instructions: | ||
subject: "Invitation instructions" | ||
hello: "Hello %{email}" | ||
someone_invited_you: "Someone has invited you to %{url}, you can accept it through the link below." | ||
accept: "Accept invitation" | ||
accept_until: "This invitation will be due in %{due_date}." | ||
ignore: "If you don't want to accept the invitation, please ignore this email. Your account won't be created until you access the link above and set your password." | ||
time: | ||
formats: | ||
devise: | ||
mailer: | ||
invitation_instructions: | ||
accept_until_format: "%B %d, %Y %I:%M %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
23 changes: 23 additions & 0 deletions
23
db/migrate/20200920102035_devise_invitable_add_to_users.rb
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,23 @@ | ||
class DeviseInvitableAddToUsers < ActiveRecord::Migration[6.0] | ||
def up | ||
change_table :users do |t| | ||
t.string :invitation_token | ||
t.datetime :invitation_created_at | ||
t.datetime :invitation_sent_at | ||
t.datetime :invitation_accepted_at | ||
t.integer :invitation_limit | ||
t.references :invited_by, polymorphic: true | ||
t.integer :invitations_count, default: 0 | ||
t.index :invitations_count | ||
t.index :invitation_token, unique: true # for invitable | ||
t.index :invited_by_id | ||
end | ||
end | ||
|
||
def down | ||
change_table :users do |t| | ||
t.remove_references :invited_by, polymorphic: true | ||
t.remove :invitations_count, :invitation_limit, :invitation_sent_at, :invitation_accepted_at, :invitation_token, :invitation_created_at | ||
end | ||
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
Oops, something went wrong.