Giter VIP home page Giter VIP logo

Comments (8)

riverrun avatar riverrun commented on July 17, 2024

I've just updated to version 0.12, and this version contains the ability to use a custom database call to Openmaize.Login.
The new module Openmaize.LoginTools show an example of how to define the function to query the database.
What you need to do is define your own query function (with 3 arguments) and then call it when using Openmaize.Login, like this (imagining your function is called Tools.query_func):

plug Openmaize.Login, [database_call: &Tools.query_func/3] when action in [:login_user]

Let me know if you have any questions / problems.

from openmaize.

 avatar commented on July 17, 2024

Thats awesome, but I'm still a little fuzzy on how to use it. Would I just create my own module with the query to get the user and then pipe it back to the check_password function to continue the process? For instance:

plug Openmaize.Login, [database_call: &Database.find_user/3] when action in [:login_user]


def module Database do

alias Openmaize.Login

blah blah

def find_user do
{query}
|> check_password

What three parameters is the find_user/3 function expecting? Thank you for the help!

from openmaize.

riverrun avatar riverrun commented on July 17, 2024

If you're using :name to identify the user in the database, find_user expects :name and "name" as the first two arguments (if you're using :email, they would be :email and "email"). The third argument will be the following map - %{"name" => name, "password" => password}
Your Database module would be something like:

defmodule Database do

  alias Openmaize.LoginTools

  def find_user(:name, "name", user_params) do
    %{"name" => name, "password" => password} = user_params
    rethink_query_func(name) # this needs to return a user model
    |> LoginTools.check_pass(password)
  end
end

And then in your controller file:

plug Openmaize.Login, [database_call: &Database.find_user/3] when action in [:login_user]

Let me know if you have any further questions.

from openmaize.

 avatar commented on July 17, 2024

So, I got it set up, but I am receiving an error when trying to log in. It is saying:
no function clause matching in Userquery.find_user/3

Here is the output from the console:
lib/App/userquery.ex:7: Userquery.find_user(:name, "name", %{"password" => "pass", "username" => "bob"})

I have this in my page_controller:

plug Openmaize.Login, [database_call: &Userquery.find_user/3] when action in [:login_user]

Here is my Userquery module:

    defmodule Userquery do

  alias Openmaize.LoginTools

  import RethinkDB.Query

  def find_user(:username, "username", user_params) do
    %{"username" => username, "password" => password} = user_params
    Query.table("users")
    |> Query.filter(%{username: username}) # this needs to return a user model
    |> LoginTools.check_pass(password)
  end
end

from openmaize.

riverrun avatar riverrun commented on July 17, 2024

In the |> Query.filter(%{username: username}) line, could you add |> IO.inspect to it (instead of the comment), and let me know what the output is?
If you could me any other info about the error, that would be really helpful.

from openmaize.

 avatar commented on July 17, 2024

Thanks for all the help. Another question:
The path for a user's profile is /profiles/show/:id, so how can I redirect to this upon login and /profiles/new upon registration? I know there are redirects in the openmaize config, but this goes beyond the examples. Thanks

from openmaize.

riverrun avatar riverrun commented on July 17, 2024

Ok, it will help people searching these issues if we close this one first. Then you can open a new issue about the redirects. Please give me a little more information about what you want to achieve.

from openmaize.

riverrun avatar riverrun commented on July 17, 2024

In the latest version in the master branch (and for versioin 0.13), I've changed this function to just cover the query, and I've changed the name of the option from database_call to query_function. The query_tools file will give you some idea of how it should now be written.

Your custom function needs to be called with two arguments - in your case it will be the username as the first argument and :username as the second argument. It then needs to return the user model (you don't have to include the check_pass call).

Sorry about making you change your code, but I think it makes more sense to restrict this function to just the query.

Let me know if you have any questions.

from openmaize.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.