Git Product home page Git Product logo

friend-ui's Introduction

Warning

This project is not used in a production environment yet, so try it at your own risk.

friendui

A Clojure library designed to wrap cemerick's friend ([https://github.com/cemerick/friend]) library. It provides templates for login / signup with email activation. Additionally there is an admin interface where one can edit existing users or add new ones.

Dependencies

  • enlive (v1.1.5) as templating library.
  • friend (0.2.1)
  • Bootstrap (> 3.0) is required too if you want it to look nice.

"Installation"

Friendui is available in Clojars: Clojars Project

Related

There is an implementation for this which uses SQL Korma as a backend: https://github.com/sventechie/friendui-sql. It uses JDBC; MySQL and PostgreSQL schemas are provided, and other DBs could be supported too if you customize the schemas.

Usage

Config

Friendui looks for a configuration file named: friendui-config.edn in the classpath. An example config can be found here: [https://github.com/sveri/friend-ui/blob/master/dev-resources/friendui-config.edn]

Then you have to alter the root binding of the base template var like this:

(:require [de.sveri.friendui.globals :as f-global])

(html/deftemplate base (str template-path "base.html")
                  [{:keys [title content]}]
                  [:#title] (util/maybe-content title)         ; this corresponds to the :base-template-title-key key in the config
                  [:#content] (util/maybe-substitute content)) ; this corresponds to the :base-template-content-key key in the config

(alter-var-root #'f-global/base-template (fn [_] (partial base)))

E-Mail

Friendui supports two different ways of sending email which are configured in friendui-config.edn by the :mail-type tag.

  1. :sendmail Expects a running sendmail on localhost by which the mail will be delivered.
  2. :smtp An extra :smtp-data map must be provided in the config which is passed as is to postal: [https://github.com/drewr/postal]

Protocol

And finally you have to implement a protocol to retrieve and store user data:

(defprotocol FrienduiStorage
  "Defines methods to access user storage for retrieval and update."
  (account-activated? [this activationid]
                      "Provides an id. Expects a boolean return value indicating if the user, belonging to the id is
                      activated or not.")
  (activate-account [this activationid]
                    "Should set the user with the given id to activated. After this function was called successfully
                    (account-activated?) should return true.")
  (create-user [this email password role activationid]
               "Should add a new user to your data store. Return value is not checked.")
  (get-all-users [this]
                 "Called from the admin view. Expects a list of all known users in this format:
                 ({:user/activated false, :user/role :user/admin, :user/email [email protected]}
                 ...)
                 Where each key corresponds to the configured keyword from friendui-config.edn")
  (get-user-for-activation-id [this id]
                              "Should return a map containing the username and role of this user like this:
                              {:username username :roles #{role}}")
  (update-user [this username data-map]
               "Updates the user with the given data map of the form: {:user/activated boolean :user/role :user/free}")
  (username-exists? [this username]
                    "Expects true if the username exists already in the storage, false otherwise."))

Then you pass this storage to the friendui routes like this:

(:require [de.sveri.friendui.routes.user :refer [friend-routes]])

(defroutes allroutes
    (friend-routes FrienduiStorageImpl)
    ...)

(def app
  (handler/site
    (friend/authenticate allroutes friend-settings)))

Your friend settings

Of course you have to define your friend settings yourself in your application, this is an example of mine:

(def friend-settings
  {:credential-fn             (partial creds/bcrypt-credential-fn user/login-user)
   :workflows                 [(workflows/interactive-form)]
   :login-uri                 "/user/login"
   :unauthorized-redirect-uri "/user/login"
   :default-landing-uri       "/"})

This should get you up and running.

Provided Resources:

(GET "/user/login")   ; expects a "username" / "password" combination
(GET "/user/signup")
(POST "/user/signup") ; expects email / password / confirm parameters
(GET "/user/accountcreated")
(GET "/user/activate/:id")
(GET "/user/accountactivated")
(GET "/user/admin" [filter])
(POST "/user/update" [username role active])
(POST "/user/add" [email password confirm]) ; used in admin view
(ANY "/user/logout")

Callback functions

Friendui provides support for callback functions. These are called under certain circumstances. You can pass them as a map to the friend-routes function like this:

(friend-routes (db/FrienduiStorageImpl db-conn) {:signup-succ-func (fn [] (println "succ func"))})

Currently these two are supported:

  • signup-succ-func Called after a successfull signup - no arguments
  • activate-account-succ-func Called after a successful user activation, takes a user map as argument (provides username and roles key)

Screenshots

Signup

Alt Signup

Signup Error

Alt Signup Error

Account Created

Alt Account Created

Admin View

Alt Admin View

Version History

  • 0.4.6 rendering with (apply str again

  • 0.4.5 Added success message when password was changed

  • 0.4.4 Removing utils and switching to clojure test

  • 0.4.3 Do not authenticate with friend after account activation

  • 0.4.2 Added SMTP authentication in config

    • Switched to postal
  • 0.4.0 Added get-loggedin-user-map function

    • accountactivated page will redirect after three seconds to index page
    • Added anti-forgery hidden input fields for forms
  • 0.3.3 Added two callback functions

  • 0.3.2 Added default unauthorized handler and an example storage protocol implementation at: sveri/friendui-datomic

  • 0.3.1 Bugfix and documentation release

  • 0.3.0 decoupled from Datomic which caused a lot of API changes.

  • 0.2.4 - Broken build - don't use it

    • Added Administrator interface for users.
    • User roles and activation status can be updated by administrators.
    • New Users can be added by administrators.
    • A filter is available for the user list
  • 0.2.3 First working release with an implementation that dependson enlive and Datomic.

License

Distributed under the Eclipse Public License either version 1.0 or any later version.

friend-ui's People

Contributors

sventech avatar sveri avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

laurio sventechie

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.