구글 OAuth2로그인 간단한 절차
전체 코드
const CLIENT_ID =
'~~~~~~~~~~~~~~~~~~~~~~~~~~~9s.apps.googleusercontent.com';
const AUTHORIZE_URI = 'https://accounts.google.com/o/oauth2/v2/auth';
@Component({
selector: 'app-nav',
templateUrl: './nav.component.html',
styleUrls: ['./nav.component.css'],
})
export class NavComponent implements OnInit {
loggedin: boolean = false;
queryStr;
loginUrl;
initQueryStr() {
this.queryStr = stringify({
response_type: 'token',
client_id: CLIENT_ID,
redirect_uri: window.location.href,
scope: 'https://www.googleapis.com/auth/contacts.readonly',
});
this.loginUrl = AUTHORIZE_URI + '?' + this.queryStr;
}
initLoggedin() {
if (window.sessionStorage.getItem('access_token')) {
this.loggedin = true;
} else {
this.loggedin = false;
}
}
constructor(
private location: Location,
private router: Router,
private authService: AuthService
) {}
goForward() {
this.location.forward();
}
postWrite() {
this.router.navigateByUrl('/postWrite');
}
logOut() {
window.sessionStorage.removeItem('access_token');
this.loggedin = false;
window.location.hash = '';
window.location.reload;
this.initQueryStr();
}
ngOnInit(): void {
this.initQueryStr();
this.initLoggedin();
const { access_token } = parse(window.location.hash.substr(1));
if (window.location.hash.substr(1).length > 5) {
window.sessionStorage.setItem(
'access_token',
window.location.hash.substr(1)
);
this.loggedin = true;
window.location.hash = '';
}
}
}