GenieFramework / GenieAuthentication.jl

Authentication plugin for Genie framework
MIT License
20 stars 11 forks source link

@authenticated! does not redirect to login page #14

Closed extremety1989 closed 2 years ago

extremety1989 commented 2 years ago

Genie v4.18.1 Julia 1.7.3

code

route("/admin", AdminController.index, named = :get_home) function index() @authenticated! html(:admin, :index, context = @__MODULE__) end i get a blank page with text saying : Redirecting you to

AbhimanyuAryan commented 2 years ago

same issue: https://github.com/GenieFramework/GenieAuthentication.jl/issues/15

rawjeev commented 2 years ago

Facing the same issue.

Julia v1.7.3 Genie v4.18.0 GenieAuthentication v1.1.0

Code Snippets

routes.jl

#Route for the admin area of the app
route("/admin/movies", AdminController.index, named = :get_home)

route("/movies/add", method = GET, named = :get_add_movie) do
  @authenticated!
  html(:movies, :movie_form)
end

AdminController.jl

module AdminController

using GenieAuthentication, Genie.Renderer, Genie.Exceptions, Genie.Renderer.Html

function index()
  println("Before @authenticated!")
  @authenticated!
  println("After @authenticated!")
  h1("Welcome Admin") |> html  
end

end

Observations

Redirection works for a while when I make some changes in AdminController.jl and try again. Like adding a println() or removing a println().

With Screenshot 2022-07-08 at 11 34 13 AM

AbhimanyuAryan commented 2 years ago

This works fine. What seems to the problem? When I login. I am redirected to h1("Welcome Admin") |> html. If you want to redirect to some different route after login redirect(:route_name)????

routes.jl

using Genie.Router
using MoviesController
using AdminController

route("/admin/movies", AdminController.index, named = :get_home)

route("/") do
  serve_static_file("welcome.html")
end

route("/movies", MoviesController.index)

# ... routes.jl
route("/movies/search", MoviesController.search, named = :search_movies)

route("/movies/search_api", MoviesController.search_api)

AdminController.jl

module AdminController

using GenieAuthentication, Genie.Renderer, Genie.Exceptions, Genie.Renderer.Html

function index()
  @authenticated!
  h1("Welcome Admin") |> html
end

end

redirect

Also I redirected to another route if logged in and hit api/movies and that worked too

module AdminController

using GenieAuthentication, Genie.Renderer, Genie.Exceptions, Genie.Renderer.Html

function index()
  @authenticated!
  redirect(:get_after_login)
end

end

routes.jl

# *** 
route("/admin/movies", AdminController.index, named = :get_home)
# ****

route("/after_login") do
  "hello"
end
AbhimanyuAryan commented 2 years ago

Also, the AuthenticationController is redirected to show the success page. You can change the code here to redirect to some other route

module AuthenticationController

using Genie, Genie.Renderer, Genie.Renderer.Html, Genie.Flash
using SearchLight
using GenieAuthentication
using ViewHelper
using Users
using Logging

function show_login()
  html(:authentication, :login, context = @__MODULE__)
end

function login()
  try
    user = findone(User, username = params(:username), password = Users.hash_password(params(:password)))
    authenticate(user.id, Genie.Sessions.session(params()))

    redirect(:success)             # <-----------------------  here it's so after login you get this...
  catch ex
    flash("Authentication failed! ")

    redirect(:show_login)
  end
end

function success()
  html(:authentication, :success, context = @__MODULE__)
end

function logout()
  deauthenticate(Genie.Sessions.session(params()))

  flash("Good bye! ")

  redirect(:show_login)
end

function show_register()
  html(:authentication, :register, context = @__MODULE__)
end

function register()
  try
    user = User(username  = params(:username),
                password  = params(:password) |> Users.hash_password,
                name      = params(:name),
                email     = params(:email)) |> save!

    authenticate(user.id, Genie.Sessions.session(params()))

    "Registration successful"
  catch ex
    @error ex

    if hasfield(typeof(ex), :msg)
      flash(ex.msg)
    else
      flash(string(ex))
    end

    redirect(:show_register)
  end
end

end
AbhimanyuAryan commented 2 years ago

@rawjeev @extremety1989 can we close this if their are no further queries?

Sov-trotter commented 2 years ago

Facing similar issues with the latest GenieAuthenticator release.

(Auth) pkg> st
     Project Auth v0.1.0
      Status `~/.julia/dev/Auth/Project.toml`
  [c43c736e] Genie v5.1.0
  [e115e502] GenieAuthentication v2.0.0
  [6d011eab] Inflector v1.0.1
  [340e8cb6] SearchLight v2.4.1
  [21a827c4] SearchLightSQLite v2.2.0
  [ade2ca70] Dates
  [56ddb016] Logging

routes.jl file

using Genie.Router
using Genie.Exceptions
using GenieAuthentication

route("/") do; @authenticated!
  serve_static_file("welcome.html")
end

Throws error while trying to load the plugin

julia> include(joinpath("plugins", "genie_authentication.jl"))
ERROR: LoadError: ArgumentError: Package AuthenticationController not found in current path:
- Run `import Pkg; Pkg.add("AuthenticationController")` to install the AuthenticationController package.

Stacktrace:
 [1] require(into::Module, mod::Symbol)
   @ Base ./loading.jl:967
 [2] include(fname::String)
   @ Base.MainInclude ./client.jl:451
 [3] top-level scope
   @ REPL[11]:1
in expression starting at /home/ezio/.julia/dev/Auth/plugins/genie_authentication.jl:3

image

 julia> Genie.Generator.newcontroller("Admin", pluralize = false)
ERROR: ArgumentError: Package Auth does not have ViewHelper in its dependencies:
- If you have Auth checked out for development and have
  added ViewHelper as a dependency but haven't updated your primary
  environment's manifest file, try `Pkg.resolve()`.
- Otherwise you may need to report an issue with Auth
Stacktrace:
 [1] require(into::Module, mod::Symbol)
   @ Base ./loading.jl:980
 [2] top-level scope
   @ ~/.julia/dev/Auth/app/resources/authentication/AuthenticationController.jl:6
in expression starting at /home/ezio/.julia/dev/Auth/app/resources/authentication/AuthenticationController.jl:6
ERROR: ArgumentError: Package Auth does not have UsersValidator in its dependencies:
- If you have Auth checked out for development and have
  added UsersValidator as a dependency but haven't updated your primary
  environment's manifest file, try `Pkg.resolve()`.
- Otherwise you may need to report an issue with Auth
Stacktrace:
 [1] require(into::Module, mod::Symbol)
   @ Base ./loading.jl:980
 [2] top-level scope
   @ ~/.julia/dev/Auth/app/resources/users/Users.jl:3
in expression starting at /home/ezio/.julia/dev/Auth/app/resources/users/Users.jl:3
ERROR: ArgumentError: Package Auth does not have ViewHelper in its dependencies:
- If you have Auth checked out for development and have
  added ViewHelper as a dependency but haven't updated your primary
  environment's manifest file, try `Pkg.resolve()`.
- Otherwise you may need to report an issue with Auth
Stacktrace:
 [1] require(into::Module, mod::Symbol)
   @ Base ./loading.jl:980
 [2] top-level scope
   @ ~/.julia/dev/Auth/app/resources/authentication/AuthenticationController.jl:6
in expression starting at /home/ezio/.julia/dev/Auth/app/resources/authentication/AuthenticationController.jl:6
[ Info: 2022-08-02 12:29:24 New controller created at /home/ezio/.julia/dev/Auth/app/resources/admin/AdminController.jl

This creates the AdminController.jl file but throws the above error.

Sov-trotter commented 2 years ago

Trying to restart the app

   ~/.julia/dev/Auth  ./bin/repl                                                                                                                                                                                                                                                             ✔  36m 10s  

 ██████╗ ███████╗███╗   ██╗██╗███████╗    ███████╗
██╔════╝ ██╔════╝████╗  ██║██║██╔════╝    ██╔════╝
██║  ███╗█████╗  ██╔██╗ ██║██║█████╗      ███████╗
██║   ██║██╔══╝  ██║╚██╗██║██║██╔══╝      ╚════██║
╚██████╔╝███████╗██║ ╚████║██║███████╗    ███████║
 ╚═════╝ ╚══════╝╚═╝  ╚═══╝╚═╝╚══════╝    ╚══════╝

| Website  https://genieframework.com
| GitHub   https://github.com/genieframework
| Docs     https://genieframework.com/docs
| Discord  https://discord.com/invite/9zyZbD6J7H
| Twitter  https://twitter.com/essenciary

Active env: DEV

Loading resources┌ Warning: 2022-08-02 12:54:06 Route named `show_login` is not defined
└ @ Genie.Router ~/.julia/packages/Genie/mMGS8/src/Router.jl:345
┌ Warning: 2022-08-02 12:54:07 Route named `show_login` is not defined
└ @ Genie.Router ~/.julia/packages/Genie/mMGS8/src/Router.jl:345
ERROR: ArgumentError: Package Auth does not have ViewHelper in its dependencies:
- If you have Auth checked out for development and have
  added ViewHelper as a dependency but haven't updated your primary
  environment's manifest file, try `Pkg.resolve()`.
- Otherwise you may need to report an issue with Auth
Stacktrace:
 [1] require(into::Module, mod::Symbol)
   @ Base ./loading.jl:980
 [2] top-level scope
   @ ~/.julia/dev/Auth/app/resources/authentication/AuthenticationController.jl:6
in expression starting at /home/ezio/.julia/dev/Auth/app/resources/authentication/AuthenticationController.jl:6
ERROR: ArgumentError: Package Auth does not have UsersValidator in its dependencies:
- If you have Auth checked out for development and have
  added UsersValidator as a dependency but haven't updated your primary
  environment's manifest file, try `Pkg.resolve()`.
- Otherwise you may need to report an issue with Auth
Stacktrace:
 [1] require(into::Module, mod::Symbol)
   @ Base ./loading.jl:980
 [2] top-level scope
   @ ~/.julia/dev/Auth/app/resources/users/Users.jl:3
in expression starting at /home/ezio/.julia/dev/Auth/app/resources/users/Users.jl:3
ERROR: ArgumentError: Package Auth does not have ViewHelper in its dependencies:
- If you have Auth checked out for development and have
  added ViewHelper as a dependency but haven't updated your primary
  environment's manifest file, try `Pkg.resolve()`.
- Otherwise you may need to report an issue with Auth
Stacktrace:
 [1] require(into::Module, mod::Symbol)
   @ Base ./loading.jl:980
 [2] top-level scope
   @ ~/.julia/dev/Auth/app/resources/authentication/AuthenticationController.jl:6
in expression starting at /home/ezio/.julia/dev/Auth/app/resources/authentication/AuthenticationController.jl:6
Loading pluginsERROR: ArgumentError: Package Auth does not have AuthenticationController in its dependencies:
- If you have Auth checked out for development and have
  added AuthenticationController as a dependency but haven't updated your primary
  environment's manifest file, try `Pkg.resolve()`.
- Otherwise you may need to report an issue with Auth
Stacktrace:
 [1] require(into::Module, mod::Symbol)
   @ Base ./loading.jl:980
 [2] top-level scope
   @ ~/.julia/dev/Auth/plugins/genie_authentication.jl:3
in expression starting at /home/ezio/.julia/dev/Auth/plugins/genie_authentication.jl:3
Loading routesERROR: ArgumentError: Package Auth does not have AdminController in its dependencies:
- If you have Auth checked out for development and have
  added AdminController as a dependency but haven't updated your primary
  environment's manifest file, try `Pkg.resolve()`.
- Otherwise you may need to report an issue with Auth
Stacktrace:
 [1] require(into::Module, mod::Symbol)
   @ Base ./loading.jl:980
 [2] top-level scope
   @ ~/.julia/dev/Auth/routes.jl:3
in expression starting at /home/ezio/.julia/dev/Auth/routes.jl:3
TxtpGame commented 2 years ago

Facing similar issues with the latest GenieAuthenticator release.

(Auth) pkg> st
     Project Auth v0.1.0
      Status `~/.julia/dev/Auth/Project.toml`
  [c43c736e] Genie v5.1.0
  [e115e502] GenieAuthentication v2.0.0
  [6d011eab] Inflector v1.0.1
  [340e8cb6] SearchLight v2.4.1
  [21a827c4] SearchLightSQLite v2.2.0
  [ade2ca70] Dates
  [56ddb016] Logging

routes.jl file

using Genie.Router
using Genie.Exceptions
using GenieAuthentication

route("/") do; @authenticated!
  serve_static_file("welcome.html")
end

Throws error while trying to load the plugin

julia> include(joinpath("plugins", "genie_authentication.jl"))
ERROR: LoadError: ArgumentError: Package AuthenticationController not found in current path:
- Run `import Pkg; Pkg.add("AuthenticationController")` to install the AuthenticationController package.

Stacktrace:
 [1] require(into::Module, mod::Symbol)
   @ Base ./loading.jl:967
 [2] include(fname::String)
   @ Base.MainInclude ./client.jl:451
 [3] top-level scope
   @ REPL[11]:1
in expression starting at /home/ezio/.julia/dev/Auth/plugins/genie_authentication.jl:3

image

 julia> Genie.Generator.newcontroller("Admin", pluralize = false)
ERROR: ArgumentError: Package Auth does not have ViewHelper in its dependencies:
- If you have Auth checked out for development and have
  added ViewHelper as a dependency but haven't updated your primary
  environment's manifest file, try `Pkg.resolve()`.
- Otherwise you may need to report an issue with Auth
Stacktrace:
 [1] require(into::Module, mod::Symbol)
   @ Base ./loading.jl:980
 [2] top-level scope
   @ ~/.julia/dev/Auth/app/resources/authentication/AuthenticationController.jl:6
in expression starting at /home/ezio/.julia/dev/Auth/app/resources/authentication/AuthenticationController.jl:6
ERROR: ArgumentError: Package Auth does not have UsersValidator in its dependencies:
- If you have Auth checked out for development and have
  added UsersValidator as a dependency but haven't updated your primary
  environment's manifest file, try `Pkg.resolve()`.
- Otherwise you may need to report an issue with Auth
Stacktrace:
 [1] require(into::Module, mod::Symbol)
   @ Base ./loading.jl:980
 [2] top-level scope
   @ ~/.julia/dev/Auth/app/resources/users/Users.jl:3
in expression starting at /home/ezio/.julia/dev/Auth/app/resources/users/Users.jl:3
ERROR: ArgumentError: Package Auth does not have ViewHelper in its dependencies:
- If you have Auth checked out for development and have
  added ViewHelper as a dependency but haven't updated your primary
  environment's manifest file, try `Pkg.resolve()`.
- Otherwise you may need to report an issue with Auth
Stacktrace:
 [1] require(into::Module, mod::Symbol)
   @ Base ./loading.jl:980
 [2] top-level scope
   @ ~/.julia/dev/Auth/app/resources/authentication/AuthenticationController.jl:6
in expression starting at /home/ezio/.julia/dev/Auth/app/resources/authentication/AuthenticationController.jl:6
[ Info: 2022-08-02 12:29:24 New controller created at /home/ezio/.julia/dev/Auth/app/resources/admin/AdminController.jl

This creates the AdminController.jl file but throws the above error.

change to import ..Main.UserApp.AuthenticationController

your version is 5.

essenciary commented 2 years ago

Confirmed, there was an issue that the @authenticated! macro did not have the path for the redirect, causing the redirect to fail.

"Fixed" in version 2.1 --> @authenticated! should be replaced with authenticated!() (macro with function with same name).

The README has been updated to reflect this.