Hello, I’m doing my Database course project using Genie as the back-end server, and I’m wondering how to set and save the session. And my efforts are as follows:
route("/api/login") do
uid = haskey(@params, :id) ? @params(:id) : ""
password = haskey(@params, :password) ? @params(:password) : ""
res = UsersController.checkLogin(uid, password, @params)
println(@params[:SESSION])
return res
end
function checkLogin(uid, password, params)
user = find(User, where("uid = ?", uid) + where("password = ?", password))
( (! haskey(params, Genie.PARAMS_SESSION_KEY) || params[Genie.PARAMS_SESSION_KEY] === nothing) ) &&
(Sessions.start(params[:REQUEST], params[:RESPONSE], params))
sess = params[:SESSION]
if length(user) != 0
return Dict("code" => 1, "msg" => "error!") |> json
else
Genie.Sessions.set!(sess, :uid, uid)
# println(params[:SESSION], "\n\n\n", params)
return Dict("code" => 0, "msg" => "success!") |> json
end
end
And I can print the @params[:SESSION]
in the route
.
However, when the browser makes a new request and I want to capture the user name in session, error occures that the @params
does not have the key.
And I wonder how to use that properly. And in django framework, I know that I can simply use request.session['uid'] = 'someid'
and get the session in the same way.
BTW, I notice that the Genie.Sessions.session
function has some bugs that when the parameter does not have a :SESSION
key, it will reture a HTTP.Request
rather than a Session
object:
function start(req::HTTP.Request, res::HTTP.Response, params::Dict{Symbol,Any} = Dict{Symbol,Any}(); options::Dict{String,String} = Dict{String,String}()) :: Tuple{HTTP.Request,HTTP.Response,Dict{Symbol,Any}}
function session(params::Dict{Symbol,Any}) :: Sessions.Session
( (! haskey(params, Genie.PARAMS_SESSION_KEY) || params[Genie.PARAMS_SESSION_KEY] === nothing) ) &&
(params[Genie.PARAMS_SESSION_KEY] = Sessions.start(params[Genie.PARAMS_REQUEST_KEY], params[Genie.PARAMS_RESPONSE_KEY])[1])
params[Genie.PARAMS_SESSION_KEY]
end