Skip to content

Commit

Permalink
[auth] add user profile
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed May 19, 2016
1 parent 306853c commit 8580afe
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
23 changes: 17 additions & 6 deletions chapters/chapter7.md
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,8 @@ npm install angular2-jwt

#### 获取用户信息

现在我们所需要做的就是发出我们的API去获取用户的信息:

```javascript
authSuccess(token) {
this.local.set('id_token', token);
Expand All @@ -648,8 +650,9 @@ authSuccess(token) {
}
```

只是我们的API似乎还不支持这样的功能。它的实现方式和我们之前的AutoComplete是一样的,也是搜索用户名:

```
```python
def list(self, request):
search_param = self.request.query_params.get('username', None)
if search_param is not None:
Expand All @@ -659,6 +662,19 @@ def list(self, request):
return Response(serializer.data)
```


然后再显示这些数据:

```
<div *ngIf="auth.authenticated()">
<div padding>
<h1>Welcome, {{ user }}</h1>
<h2 *ngIf="user_info">Last login {{user_info.last_login}}</h2>
<button block (click)="logout()">Logout</button>
</div>
</div>
```

创建博客
---

Expand Down Expand Up @@ -691,8 +707,3 @@ class BlogpostSet(viewsets.ModelViewSet):
serializer_class = BlogpsotSerializer

```


TODO
---

23 changes: 17 additions & 6 deletions growth.md
Original file line number Diff line number Diff line change
Expand Up @@ -2508,6 +2508,8 @@ npm install angular2-jwt

#### 获取用户信息

现在我们所需要做的就是发出我们的API去获取用户的信息:

```javascript
authSuccess(token) {
this.local.set('id_token', token);
Expand All @@ -2526,8 +2528,9 @@ authSuccess(token) {
}
```

只是我们的API似乎还不支持这样的功能。它的实现方式和我们之前的AutoComplete是一样的,也是搜索用户名:

```
```python
def list(self, request):
search_param = self.request.query_params.get('username', None)
if search_param is not None:
Expand All @@ -2537,6 +2540,19 @@ def list(self, request):
return Response(serializer.data)
```


然后再显示这些数据:

```
<div *ngIf="auth.authenticated()">
<div padding>
<h1>Welcome, {{ user }}</h1>
<h2 *ngIf="user_info">Last login {{user_info.last_login}}</h2>
<button block (click)="logout()">Logout</button>
</div>
</div>
```

创建博客
---

Expand Down Expand Up @@ -2570,11 +2586,6 @@ class BlogpostSet(viewsets.ModelViewSet):

```


TODO
---


Mobile Web
===

Expand Down
26 changes: 17 additions & 9 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ <h2>全栈增长工程师实战目录</h2>
<li><a href="#profile-1">Profile</a></li>
</ul></li>
<li><a href="#创建博客">创建博客</a></li>
<li><a href="#todo">TODO</a></li>
</ul></li>
<li><a href="#mobile-web">Mobile Web</a><ul>
<li><a href="#移动设备处理">移动设备处理</a></li>
Expand Down Expand Up @@ -1953,6 +1952,7 @@ <h4 id="authhttp">AuthHttp</h4>
<span class="op">}</span>)</code></pre></div>
<p>现在,我们就可以用和Http一样的方式去获取用户信息。</p>
<h4 id="获取用户信息">获取用户信息</h4>
<p>现在我们所需要做的就是发出我们的API去获取用户的信息:</p>
<div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="at">authSuccess</span>(token) <span class="op">{</span>
<span class="kw">this</span>.<span class="va">local</span>.<span class="at">set</span>(<span class="st">&#39;id_token&#39;</span><span class="op">,</span> token)<span class="op">;</span>
<span class="kw">this</span>.<span class="at">user</span> <span class="op">=</span> <span class="kw">this</span>.<span class="va">jwtHelper</span>.<span class="at">decodeToken</span>(token).<span class="at">username</span><span class="op">;</span>
Expand All @@ -1968,13 +1968,22 @@ <h4 id="获取用户信息">获取用户信息</h4>
err <span class="op">=&gt;</span> <span class="va">console</span>.<span class="at">log</span>(err)
)<span class="op">;</span>
<span class="op">}</span></code></pre></div>
<pre><code>def list(self, request):
search_param = self.request.query_params.get(&#39;username&#39;, None)
if search_param is not None:
queryset = User.objects.filter(username__contains=search_param)

serializer = UserSerializer(queryset, many=True)
return Response(serializer.data)</code></pre>
<p>只是我们的API似乎还不支持这样的功能。它的实现方式和我们之前的AutoComplete是一样的,也是搜索用户名:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> <span class="bu">list</span>(<span class="va">self</span>, request):
search_param <span class="op">=</span> <span class="va">self</span>.request.query_params.get(<span class="st">&#39;username&#39;</span>, <span class="va">None</span>)
<span class="cf">if</span> search_param <span class="op">is</span> <span class="op">not</span> <span class="va">None</span>:
queryset <span class="op">=</span> User.objects.<span class="bu">filter</span>(username__contains<span class="op">=</span>search_param)

serializer <span class="op">=</span> UserSerializer(queryset, many<span class="op">=</span><span class="va">True</span>)
<span class="cf">return</span> Response(serializer.data)</code></pre></div>
<p>然后再显示这些数据:</p>
<pre><code>&lt;div *ngIf=&quot;auth.authenticated()&quot;&gt;
&lt;div padding&gt;
&lt;h1&gt;Welcome, {{ user }}&lt;/h1&gt;
&lt;h2 *ngIf=&quot;user_info&quot;&gt;Last login {{user_info.last_login}}&lt;/h2&gt;
&lt;button block (click)=&quot;logout()&quot;&gt;Logout&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;</code></pre>
<h2 id="创建博客">创建博客</h2>
<p>权限管理</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">SAFE_METHODS <span class="op">=</span> [<span class="st">&#39;GET&#39;</span>, <span class="st">&#39;HEAD&#39;</span>, <span class="st">&#39;OPTIONS&#39;</span>]
Expand All @@ -2001,7 +2010,6 @@ <h2 id="创建博客">创建博客</h2>
permission_classes <span class="op">=</span> (permissions.IsAuthenticatedOrReadOnly,)
queryset <span class="op">=</span> Blogpost.objects.<span class="bu">all</span>()
serializer_class <span class="op">=</span> BlogpsotSerializer</code></pre></div>
<h2 id="todo">TODO</h2>
<h1 id="mobile-web">Mobile Web</h1>
<h2 id="移动设备处理">移动设备处理</h2>
<pre><code>pip install django_mobile</code></pre>
Expand Down

0 comments on commit 8580afe

Please sign in to comment.