Sha256: c0b10cbdaa696d0d8722a00193af0362442d3644fbf5dc73e6fdf33f3dc00ffd

Contents?: true

Size: 1.09 KB

Versions: 16

Compression:

Stored size: 1.09 KB

Contents

# Example for Authenticating a User

in routes.rb
```rb
Rails.application.routes.draw do
  # ...... your other routes
  unless Rails.env.production?
    scope path: "/__cypress__", controller: 'cypress' do
      post "forceLogin", action: 'force_login'
    end
  end
end
```

app/controllers/cypress_controller.rb
```rb
class CypressController < ApplicationController
  skip_before_action :verify_authenticity_token

  def force_login
    if params[:email].present?
      user = User.find_by!(email: params.require(:email))
    else
      user = User.first!
    end
    sign_in(user)
    redirect_to URI.parse(params.require(:redirect_to)).path
  end
end
```

in cypress/support/on-rails.js
```js
Cypress.Commands.add("forceLogin", (details) => {
  if(!details)
    details = {}

  if(!details.redirect_to)
    details.redirect_to = '/'

  cy.visit("__cypress__/forceLogin",
    { method: "POST", body: { email: details.email, redirect_to: details.redirect_to }  })
});
```

examples of usage in cypress specs
```js
cy.forceLogin()
cy.forceLogin({redirect_to: '/profile'})
cy.forceLogin({email: 'someuser@mail.com'})
```

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
cypress-on-rails-1.13.1 docs/authentication.md
cypress-on-rails-1.13.0 docs/authentication.md
cypress-on-rails-1.12.1 docs/authentication.md
cypress-on-rails-1.12.0 docs/authentication.md
cypress-on-rails-1.11.0 docs/authentication.md
cypress-on-rails-1.10.1 docs/authentication.md
cypress-on-rails-1.9.1 docs/authentication.md
cypress-on-rails-1.9.0 docs/authentication.md
cypress-on-rails-1.8.1 docs/authentication.md
cypress-on-rails-1.8.0 docs/authentication.md
cypress-on-rails-1.7.0 docs/authentication.md
cypress-on-rails-1.6.0 docs/authentication.md
cypress-on-rails-1.5.1 docs/authentication.md
cypress-on-rails-1.5.0 docs/authentication.md
cypress-on-rails-1.4.2 docs/authentication.md
cypress-on-rails-1.4.1 docs/authentication.md