Namdak Tonpa (maxim) wrote,
Namdak Tonpa
maxim

Как в N2O выглядит страница логина

Вынес все методы логина в отдельную библиотеку: https://github.com/voxoz/avz

AVZ -- это наша библиотека, которая поддерживает JavaScript-based login (Google, Facebook) и HTTP redirect-based (Twitter, Github) с довольно простым и вмеяемым API:

sdk/0                % JavaScript for page embedding for JavaScript based login methods
login_button/0       % HTML Button for page embedding
event/1              % Page Event for HTTP redirect based login methods
api_event/3          % Page Event for JavaScript based login methods
callback/0           % Callback part of HTTP redirect based login methods
registration_data/3  % Process Parameters. 


Вот например как выглядит код который отвечает за Google авторизацию: https://github.com/voxoz/avz/blob/master/src/google.erl

-module(google).
-author('Andrii Zadorozhnii').
-include_lib("avz/include/avz.hrl").
-include_lib("n2o/include/wf.hrl").
-include_lib("kvs/include/users.hrl").
-compile(export_all).
-export(?API).
-define(GPLUS_CLIENT_ID, case application:get_env(web, gplus_client_id) of {ok, Id} -> Id; _-> "" end).
-define(GPLUS_COOKIE_POLICY, case application:get_env(web, gplus_cookiepolicy) of {ok, P} -> P; _-> "" end).

api_event(plusLogin, Args, _)-> JSArgs = n2o_json:decode(Args), avz:login(google, JSArgs#struct.lst).

registration_data(Props, google, Ori)->
    Id = proplists:get_value(<<"id">>, Props),
    Name = proplists:get_value(<<"name">>, Props),
    GivenName = proplists:get_value(<<"givenName">>, Name#struct.lst),
    FamilyName = proplists:get_value(<<"familyName">>, Name#struct.lst),
    Image = proplists:get_value(<<"image">>, Props),
    {Id, Ori#user{ display_name = proplists:get_value(<<"displayName">>, Props),
                   avatar = lists:nth(1,string:tokens(
                       binary_to_list(proplists:get_value(<<"url">>, Image#struct.lst)), "?")),
                   email = email_prop(Props,google),
                   name = GivenName,
                   surname = FamilyName,
                   googleplus_id = Id,
                   register_date = erlang:now(),
                   sex = proplists:get_value(gender, Props),
                   status = ok }}.

email_prop(Props, _) -> binary_to_list(proplists:get_value(<<"email">>, Props)).

login_button()-> #panel{id=plusloginbtn, class=["btn-group"], body=
    #link{class=[btn, "btn-google-plus", "btn-large"], 
        body=[#i{class=["icon-google-plus", "icon-large"]}, <<"Google">>] }}.

event(_) -> ok.
callback() -> ok.
sdk() ->
    wf:wire(#api{name=plusLogin, tag=plus}),
    #dtl{bind_script=false, file="google_sdk", ext="dtl", folder="priv/static/js",
        bindings=[{loginbtnid, plusloginbtn},{clientid, ?GPLUS_CLIENT_ID},{cookiepolicy, ?GPLUS_COOKIE_POLICY}]}.


Теперь, например, вот эта страница: http://skyline.synrc.com/



Выглядит вот так: https://github.com/5HT/skyline/blob/master/apps/web/src/login.erl

-module(login).
-compile(export_all).
-include_lib("avz/include/avz.hrl").
-include_lib("n2o/include/wf.hrl").
-include_lib("kvs/include/users.hrl").

main() ->
  avz:callbacks(?METHODS),
  [ #dtl{file = "login",  ext="dtl",bindings=[{title,<<"Login">>},
                                              {header,index:header()},
                                              {footer,index:footer()},
                                              {sdk,avz:sdk(?METHODS)},
                                              {buttons,avz:buttons(?METHODS)}]} ].

event(X) -> avz:event(X).
api_event(X,Y,Z) -> avz:api_event(X,Y,Z).
Tags: cs
Subscribe
  • Post a new comment

    Error

    default userpic

    Your IP address will be recorded 

  • 11 comments