feat: github oauth login

This commit is contained in:
Rongjian Zhang
2019-02-07 14:35:19 +08:00
parent 5c4d29c522
commit 543f8c82ea
31 changed files with 749 additions and 625 deletions

View File

@@ -1,71 +1 @@
import 'dart:convert';
import 'dart:async';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:github/server.dart';
export 'package:github/server.dart';
import '../token.dart';
var ghClient = createGitHubClient(auth: Authentication.withToken(token));
final prefix = 'https://api.github.com';
final endpoint = '/graphql';
Future<dynamic> getWithCredentials(String url, {String contentType}) async {
var headers = {HttpHeaders.authorizationHeader: 'token $token'};
if (contentType != null) {
// https://developer.github.com/v3/repos/contents/#custom-media-types
headers[HttpHeaders.contentTypeHeader] = contentType;
}
final res = await http.get(
prefix + url,
headers: headers,
);
final data = json.decode(res.body);
return data;
}
Future<dynamic> postWithCredentials(String url, String body,
{String contentType}) async {
var headers = {HttpHeaders.authorizationHeader: 'token $token'};
if (contentType != null) {
headers[HttpHeaders.contentTypeHeader] = contentType;
}
final res = await http.post(prefix + url, headers: headers, body: body);
final data = json.decode(res.body);
return data;
}
Future<dynamic> putWithCredentials(String url,
{String contentType, String body}) async {
var headers = {HttpHeaders.authorizationHeader: 'token $token'};
if (contentType != null) {
headers[HttpHeaders.contentTypeHeader] = contentType;
}
final res = await http.put(prefix + url, headers: headers, body: body ?? {});
final data = json.decode(res.body);
return data;
}
Future<dynamic> patchWithCredentials(String url) async {
var headers = {HttpHeaders.authorizationHeader: 'token $token'};
await http.patch(prefix + url, headers: headers);
return true;
}
Future<dynamic> query(String query) async {
final res =
await postWithCredentials('/graphql', json.encode({'query': query}));
if (res['errors'] != null) {
throw new Exception(res['errors'].toString());
}
// print(res);
return res['data'];
}
Future<List<Event>> fetchEvents(int page) async {
List data = await getWithCredentials(
'/users/pd4d10/received_events/public?page=$page',
);
return data.map<Event>((item) => Event.fromJSON(item)).toList();
}

View File

@@ -2,11 +2,15 @@ import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import '../providers/settings.dart';
import '../screens/screens.dart';
import '../screens/repo.dart';
export 'github.dart';
export 'octicons.dart';
export 'timeago.dart';
// These keys are for development
var clientId = '9b7d1cc04a1db5710767';
var clientSecret = '710e085908dde6a8b55f7a9dc447ad5c0c5617d1';
Color convertColor(String cssHex) {
if (cssHex.startsWith('#')) {
cssHex = cssHex.substring(1);
@@ -21,8 +25,8 @@ class Option<T> {
}
Future<bool> showConfim(BuildContext context, String text) {
switch (SettingsProvider.of(context).layout) {
case LayoutMap.cupertino:
switch (SettingsProvider.of(context).theme) {
case ThemeMap.cupertino:
return showCupertinoDialog(
context: context,
builder: (context) {
@@ -89,8 +93,8 @@ Future<T> showOptions<T>(BuildContext context, List<Option<T>> options) {
);
};
switch (SettingsProvider.of(context).layout) {
case LayoutMap.cupertino:
switch (SettingsProvider.of(context).theme) {
case ThemeMap.cupertino:
return showCupertinoDialog<T>(
context: context,
builder: builder,