diff --git a/lib/generators/auth/templates/models/user.rb b/lib/generators/auth/templates/models/user.rb new file mode 100644 index 0000000..df6800a --- /dev/null +++ b/lib/generators/auth/templates/models/user.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +#= User +# +# A person who has an account on the site (via Google Cloud Identity Platform). +class User < ApplicationRecord + validates :identity_platform_id, presence: true, uniqueness: true + + def self.from_identity_token(token) + return unless token.valid? + + find_or_initialize_by(identity_platform_id: token.subject).tap do |user| + user.update! token.payload.slice(:name, :email) + end + end +end diff --git a/lib/generators/auth/templates/models/user_migration.rb b/lib/generators/auth/templates/models/user_migration.rb new file mode 100644 index 0000000..083acef --- /dev/null +++ b/lib/generators/auth/templates/models/user_migration.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +class CreateUsers < ActiveRecord::Migration[7.0] + def change + create_table :users do |t| + t.string :email + t.string :name + t.string :identity_platform_id, null: false + + t.timestamps + + t.index %i[identity_platform_id], name: :UK_user_identity_platform, unique: true + end + end +end + \ No newline at end of file