Skip to content

Commit

Permalink
Merge pull request #23 from samsam2310/master
Browse files Browse the repository at this point in the history
add FBlogin and FB logout
  • Loading branch information
samsam2310 committed Jan 2, 2015
2 parents d76d38d + 16817cd commit a8d3eee
Show file tree
Hide file tree
Showing 10 changed files with 431 additions and 409 deletions.
8 changes: 5 additions & 3 deletions basehandler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#coding=utf-8
import tornado.web
import logging

Expand All @@ -9,13 +10,14 @@ class BaseHandler(tornado.web.RequestHandler):
def get_current_user(self):
uid = self.get_secure_cookie("uid")
if not uid:
return None
return None
user = memcache.get('USER_'+uid)
if not user:
user = User.by_id(int(uid))
return user
def render(self,*a,**kw):
kw['FACEBOOK_APP_ID']=APP_Key.get('FACEBOOK_APP_ID')
kw['FACEBOOK_APP_ID'] = APP_Key.get('FACEBOOK_APP_ID')
kw['current_user'] = self.current_user
super(BaseHandler, self).render(*a,**kw)
def error(self,error):
raise tornado.web.HTTPError(error)
raise tornado.web.HTTPError(error)
16 changes: 11 additions & 5 deletions login.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#coding=utf-8
import logging

from basehandler import BaseHandler
Expand All @@ -6,16 +7,17 @@

class LoginPage(BaseHandler):
def post(self):
fb_login = self.get_argument(name='facebook',default='')
if fb_login == 1:
fb_login = self.get_argument(name='fblogin',default='')
if fb_login == '1':
#Login with FB,
#if user doesn't exist, sign up with FB,
#if user doesn't sign in with FB,return 403.
cookies = dict((n, self.cookies[n].value) for n in self.cookies.keys())
fb_cookie = users.get_fb_cookie(cookies)
logging.error(cookies)
#if user doesn't sign in with FB,return 403.
if not fb_cookie:
return self.HTTPError(403)
return self.error(403)
user = users.check_facebook_user(fb_cookie)
#if user doesn't exist, sign up with FB,
if not user:
user = users.new_facebook_user(fb_cookie)
self.set_secure_cookie('uid',str(user.key.id()))
Expand All @@ -30,3 +32,7 @@ def post(self):
self.error(404)
# --------end---Needs to be done.-------

class LogoutPage(BaseHandler):
def get(self):
self.set_secure_cookie('uid','')
return self.redirect('/')
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import tornado.webimport tornado.wsgiimport osimport tool.configimport loggingfrom basehandler import BaseHandlerfrom login import LoginPageclass MainHandler(BaseHandler): def get(self): if self.current_user: self.write("Hello, %s!!!"%self.current_user.name) else: self.render("twcl.html")class DBinit(tornado.web.RequestHandler): def get(self): tool.config.debug_init() settings = { 'template_path' : os.path.join(os.path.dirname(__file__), 'templates'), 'cookie_secret' : tool.config.APP_Key.get('cookie_secret'), "login_url" : "/login", 'debug' : False,}tornado_app = tornado.web.Application([ (r'/login', LoginPage), (r'/dbinit',DBinit), (r"/", MainHandler),],**settings)app = tornado.wsgi.WSGIAdapter(tornado_app)
#coding=utf-8import tornado.webimport tornado.wsgiimport osimport tool.configimport loggingfrom basehandler import BaseHandlerfrom login import LoginPagefrom login import LogoutPageclass MainHandler(BaseHandler): def get(self): self.render("home.html")# class DBinit(tornado.web.RequestHandler):# def get(self):# tool.config.debug_init() settings = { 'template_path' : os.path.join(os.path.dirname(__file__), 'templates'), 'cookie_secret' : tool.config.APP_Key.get('cookie_secret'), "login_url" : "/login", 'debug' : False,}tornado_app = tornado.web.Application([ (r'/login', LoginPage), (r'/logout', LogoutPage), # (r'/dbinit', DBinit), (r"/", MainHandler),],**settings)app = tornado.wsgi.WSGIAdapter(tornado_app)
Expand Down
158 changes: 158 additions & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>TWCL</title>

<!-- font -->
<link href='http://fonts.googleapis.com/css?family=Audiowide' rel='stylesheet' type='text/css'>

<!-- Bootstrap Core CSS -->
<link href="/static/twcl_page/css/bootstrap.min.css" rel="stylesheet">

<!-- Custom CSS -->
<link href="/static/twcl_page/css/agency.css" rel="stylesheet">

<!-- Custom Fonts -->
<link href="/static/twcl_page/font-awesome-4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet" type="text/css">
<link href='http://fonts.googleapis.com/css?family=Kaushan+Script' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Roboto+Slab:400,100,300,700' rel='stylesheet' type='text/css'>

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

<!-- template Link -->
{% block Link %}
{% end %}

</head>

<body id="page-top" class="index">

<!-- ___________________________FB_API____________________________ -->
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({appId: '{{FACEBOOK_APP_ID}}', status: true,
cookie: true, xfbml: true,oauth: true});
};
(function() {
var e = document.createElement('script');
e.type = 'text/javascript';
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
<!-- ___________________________FB_API____________________________ -->


<!-- Navigation -->
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header page-scroll">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand page-scroll" href="#page-top" style="font-family: 'Audiowide', cursive;">TWCL</a>
</div>

<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li class="hidden">
<a href="#page-top"></a>
</li>
<li>
<a class="page-scroll" href="#about">About</a>
</li>
<li>
<a class="page-scroll" href="#portfolio">Members</a>
</li>
<li>
<a class="page-scroll" href="#contact">Contact</a>
</li>
{% if current_user %}
<li>
<div class="btn btn-group">
<a class="btn btn-link dropdown-toggle" data-toggle="dropdown" href="#">
{{current_user.name}}
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li>
<a href="/logout">Logout</a>
</li>
</ul>
</div>
</li>
{% else %}
<li>
<a class="btn" data-toggle="modal" data-target=".login">Login</a>
</li>
{% end %}
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>

<!-- Header -->
{% block Header %}
{% end %}

<!-- About Section -->
{% block Section %}
{% end %}

<!-- Footer -->
{% block Footer %}
{% end %}

<!-- Portfolio Modals -->
<!-- Use the modals below to showcase details about your portfolio projects! -->

{% block Modals %}
{% end %}

<!-- jQuery -->
<script src="/static/twcl_page/js/jquery.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="/static/twcl_page/js/bootstrap.min.js"></script>

<!-- Plugin JavaScript -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>
<script src="/static/twcl_page/js/classie.js"></script>
<script src="/static/twcl_page/js/cbpAnimatedHeader.js"></script>

<!-- Contact Form JavaScript -->
<!--<script src="/static/twcl_page/js/jqBootstrapValidation.js"></script>
<script src="/static/twcl_page/js/contact_me.js"></script>-->

<!-- Custom Theme JavaScript -->
<!-- <script src="/static/twcl_page/js/agency.js"></script>-->

<!-- template Javascript -->
{% block Javascript %}
{% end %}

</body>
</html>
Loading

0 comments on commit a8d3eee

Please sign in to comment.