# frozen-string-literal: truemoduleRodauthFeature.define(:verify_account,:VerifyAccount)dodepends:login,:create_account,:email_baseerror_flash"Unable to verify account"error_flash"Unable to resend verify account email",'verify_account_resend'error_flash"An email has recently been sent to you with a link to verify your account",'verify_account_email_recently_sent'error_flash"There was an error verifying your account: invalid verify account key",'no_matching_verify_account_key'error_flash"The account you tried to create is currently awaiting verification",'attempt_to_create_unverified_account'error_flash"The account you tried to login with is currently awaiting verification",'attempt_to_login_to_unverified_account'notice_flash"Your account has been verified"notice_flash"An email has been sent to you with a link to verify your account",'verify_account_email_sent'loaded_templates%w'verify-account verify-account-resend verify-account-email'view'verify-account','Verify Account'view'verify-account-resend','Resend Verification Email','resend_verify_account'additional_form_tagsadditional_form_tags'verify_account_resend'afterafter'verify_account_email_resend'beforebefore'verify_account_email_resend'button'Verify Account'button'Send Verification Email Again','verify_account_resend'redirectredirect(:verify_account_email_sent){default_post_email_redirect}redirect(:verify_account_email_recently_sent){default_post_email_redirect}translatable_method:verify_account_email_subject,'Verify Account'auth_value_method:verify_account_key_param,'key'auth_value_method:verify_account_autologin?,trueauth_value_method:verify_account_table,:account_verification_keysauth_value_method:verify_account_id_column,:idauth_value_method:verify_account_email_last_sent_column,:email_last_sentauth_value_method:verify_account_skip_resend_email_within,300auth_value_method:verify_account_key_column,:keytranslatable_method:verify_account_resend_explanatory_text,"<p>If you no longer have the email to verify the account, you can request that it be resent to you:</p>"translatable_method:verify_account_resend_link_text,"Resend Verify Account Information"session_key:verify_account_session_key,:verify_account_keyauth_value_method:verify_account_set_password?,trueauth_methods(:allow_resending_verify_account_email?,:create_verify_account_key,:create_verify_account_email,:get_verify_account_key,:get_verify_account_email_last_sent,:remove_verify_account_key,:send_verify_account_email,:set_verify_account_email_last_sent,:verify_account,:verify_account_email_body,:verify_account_email_link,:verify_account_key_insert_hash,:verify_account_key_value)auth_private_methods(:account_from_verify_account_key)route(:verify_account_resend)do|r|verify_account_check_already_logged_inbefore_verify_account_resend_router.getdoresend_verify_account_viewendr.postdoverified=falseifaccount_from_login(param(login_param))&&allow_resending_verify_account_email?ifverify_account_email_recently_sent?set_redirect_error_flashverify_account_email_recently_sent_error_flashredirectverify_account_email_recently_sent_redirectendbefore_verify_account_email_resendifverify_account_email_resendafter_verify_account_email_resendverified=trueendendifverifiedset_notice_flashverify_account_email_sent_notice_flashelseset_redirect_error_status(no_matching_login_error_status)set_redirect_error_flashverify_account_resend_error_flashendredirectverify_account_email_sent_redirectendendroutedo|r|verify_account_check_already_logged_inbefore_verify_account_route@password_field_autocomplete_value='new-password'r.getdoifkey=param_or_nil(verify_account_key_param)set_session_value(verify_account_session_key,key)redirect(r.path)endifkey=session[verify_account_session_key]ifaccount_from_verify_account_key(key)verify_account_viewelseremove_session_value(verify_account_session_key)set_redirect_error_flashno_matching_verify_account_key_error_flashredirectrequire_login_redirectendendendr.postdokey=session[verify_account_session_key]||param(verify_account_key_param)unlessaccount_from_verify_account_key(key)set_redirect_error_status(invalid_key_error_status)set_redirect_error_flashverify_account_error_flashredirectverify_account_redirectendcatch_errordoifverify_account_set_password?password=param(password_param)ifrequire_password_confirmation?&&password!=param(password_confirm_param)throw_error_status(unmatched_field_error_status,password_param,passwords_do_not_match_message)endunlesspassword_meets_requirements?(password)throw_error_status(invalid_field_error_status,password_param,password_does_not_meet_requirements_message)endendtransactiondobefore_verify_accountverify_accountifverify_account_set_password?set_password(password)endremove_verify_account_keyafter_verify_accountendifverify_account_autologin?autologin_session('verify_account')endremove_session_value(verify_account_session_key)set_notice_flashverify_account_notice_flashredirectverify_account_redirectendset_error_flashverify_account_error_flashverify_account_viewendenddefrequire_login_confirmation?falseenddefallow_resending_verify_account_email?account[account_status_column]==account_unverified_status_valueenddefremove_verify_account_keyverify_account_ds.deleteenddefverify_accountupdate_account(account_status_column=>account_open_status_value)==1enddefverify_account_email_resendif@verify_account_key_value=get_verify_account_key(account_id)set_verify_account_email_last_sentsend_verify_account_emailtrueendenddefcreate_account_notice_flashverify_account_email_sent_notice_flashenddefnew_account(login)ifaccount_from_login(login)&&allow_resending_verify_account_email?set_redirect_error_status(unopen_account_error_status)set_error_flashattempt_to_create_unverified_account_error_flashresponse.writeresend_verify_account_viewrequest.haltendsuperenddefaccount_from_verify_account_key(key)@account=_account_from_verify_account_key(key)enddefaccount_initial_status_valueaccount_unverified_status_valueenddefsend_verify_account_emailsend_email(create_verify_account_email)enddefverify_account_email_linktoken_link(verify_account_route,verify_account_key_param,verify_account_key_value)enddefget_verify_account_key(id)verify_account_ds(id).get(verify_account_key_column)enddefskip_status_checks?falseenddefcreate_account_autologin?falseenddefcreate_account_set_password?returnfalseifverify_account_set_password?superenddefset_verify_account_email_last_sentverify_account_ds.update(verify_account_email_last_sent_column=>Sequel::CURRENT_TIMESTAMP)ifverify_account_email_last_sent_columnenddefget_verify_account_email_last_sentifcolumn=verify_account_email_last_sent_columnifts=verify_account_ds.get(column)convert_timestamp(ts)endendenddefsetup_account_verificationgenerate_verify_account_key_valuecreate_verify_account_keysend_verify_account_emailendprivatedef_login_form_footer_linkslinks=superif!param_or_nil(login_param)||((account||account_from_login(param(login_param)))&&allow_resending_verify_account_email?)links<<[30,verify_account_resend_path,verify_account_resend_link_text]endlinksenddefverify_account_email_recently_sent?(email_last_sent=get_verify_account_email_last_sent)&&(Time.now-email_last_sent<verify_account_skip_resend_email_within)endattr_reader:verify_account_key_valuedefbefore_login_attemptunlessopen_account?set_redirect_error_status(unopen_account_error_status)set_error_flashattempt_to_login_to_unverified_account_error_flashresponse.writeresend_verify_account_viewrequest.haltendsuperenddefafter_create_accountsetup_account_verificationsuperenddefverify_account_check_already_logged_incheck_already_logged_inenddefgenerate_verify_account_key_value@verify_account_key_value=random_keyenddefcreate_verify_account_keyds=verify_account_dstransactiondoifds.empty?ife=raised_uniqueness_violation{ds.insert(verify_account_key_insert_hash)}# If inserting into the verify account table causes a violation, we can pull the # key from the verify account table, or reraise.raiseeunless@verify_account_key_value=get_verify_account_key(account_id)endendendenddefverify_account_key_insert_hash{verify_account_id_column=>account_id,verify_account_key_column=>verify_account_key_value}enddefcreate_verify_account_emailcreate_email(verify_account_email_subject,verify_account_email_body)enddefverify_account_email_bodyrender('verify-account-email')enddefverify_account_ds(id=account_id)db[verify_account_table].where(verify_account_id_column=>id)enddef_account_from_verify_account_key(token)account_from_key(token,account_unverified_status_value){|id|get_verify_account_key(id)}endendend