Skip to content

Commit 3b69153

Browse files
committed
init
0 parents  commit 3b69153

25 files changed

+397
-0
lines changed

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# If you need to exclude files such as those generated by an IDE, use
2+
# $GIT_DIR/info/exclude or the core.excludesFile configuration variable as
3+
# described in https://git-scm.com/docs/gitignore
4+
5+
*.egg-info
6+
*.pot
7+
*.py[co]
8+
.tox/
9+
__pycache__
10+
MANIFEST
11+
dist/
12+
docs/_build/
13+
docs/locale/
14+
node_modules/
15+
tests/coverage_html/
16+
tests/.coverage
17+
build/
18+
tests/report/

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Django Oauth Demo
2+
3+
## Package
4+
使用 python-social-auth (django-social-auth is deprecated)
5+
6+
installation:
7+
pip install -r requirements.txt
8+
9+
優點:將 Oauth User model 整合,並且要實作 account method, 修改 template 相較於 django-allauth 都很簡單。
10+
11+
12+
## Run Demo
13+
14+
1. sync database model
15+
$ ./manage.py migrate
16+
17+
2. run server
18+
$ ./manage.py runserver
19+
20+
3. access endpoint
21+
using browser: http://localhost:8000/accounts/login
22+
23+
24+
## Endpoints
25+
- /accounts/login
26+
目前只支援 Oauth Login<br>
27+
在 Facebook Login 的部分,Domain name 不可以使用 127.0.0.1:8000<br>
28+
要改用 localhost:8000/accounts/login
29+
Oauth 登入成功後,會導入 "localhost:8000" home page
30+
31+
- /accounts/logout
32+
- /accounts/info
33+
console 顯示 username & user email, FB Oauth 不會取得 Email!
34+
35+
36+
## Apps
37+
38+
- accounts
39+
用於管理基本帳戶操作,目前只有實作 Google, Facebook 登入

oauth_demo/account/__init__.py

Whitespace-only changes.

oauth_demo/account/admin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

oauth_demo/account/apps.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class AccountConfig(AppConfig):
5+
name = 'account'

oauth_demo/account/migrations/__init__.py

Whitespace-only changes.

oauth_demo/account/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.db import models
2+
3+
# Create your models here.
4+
class UserProfile(models.Model):
5+
pass

oauth_demo/account/tests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

oauth_demo/account/urls.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""oauth_demo URL Configuration
2+
3+
The `urlpatterns` list routes URLs to views. For more information please see:
4+
https://docs.djangoproject.com/en/1.10/topics/http/urls/
5+
Examples:
6+
Function views
7+
1. Add an import: from my_app import views
8+
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
9+
Class-based views
10+
1. Add an import: from other_app.views import Home
11+
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
12+
Including another URLconf
13+
1. Import the include() function: from django.conf.urls import url, include
14+
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
15+
"""
16+
from django.conf.urls import url, include
17+
from .views import sign_in, sign_out, get_user_info
18+
19+
urlpatterns = [
20+
url(r'^info/$', get_user_info),
21+
url(r'^login/$', sign_in),
22+
url(r'^logout', sign_out),
23+
url(r'^', include('social_django.urls', namespace='social'))
24+
]

oauth_demo/account/views.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from django.shortcuts import render_to_response, redirect
2+
from django.http import HttpResponse
3+
from django.contrib.auth import logout
4+
from django.contrib.auth.decorators import login_required
5+
6+
7+
def sign_in(request):
8+
if request.method == "GET":
9+
return render_to_response('login.html')
10+
else:
11+
# processing signin action
12+
13+
# authenticate & exception handling
14+
15+
# status 未定
16+
return HttpResponse(status=200)
17+
18+
19+
20+
def sign_out(request):
21+
if request.user.is_authenticated:
22+
print ("user has been login")
23+
logout(request)
24+
25+
return redirect("/")
26+
27+
28+
def get_user_info(request):
29+
if request.user.is_authenticated:
30+
print (request.user.username, request.user.email)
31+
else:
32+
print ("Anoymous user")
33+
34+
return HttpResponse(status=200)

0 commit comments

Comments
 (0)