Skip to content

Instantly share code, notes, and snippets.

@jangia
Last active November 10, 2020 18:41
Show Gist options
  • Select an option

  • Save jangia/c18c89709b87b6caef5497e5a572b045 to your computer and use it in GitHub Desktop.

Select an option

Save jangia/c18c89709b87b6caef5497e5a572b045 to your computer and use it in GitHub Desktop.
<script>
import Auth from '@aws-amplify/auth';
import { onMount } from 'svelte';
import axios from 'axios';
Auth.configure(
{
Auth: {
region: 'region',
userPoolId: 'pool_id',
userPoolWebClientId: 'client_id'
}
}
);
async function get_access_token() {
const { accessToken } = await Auth.currentSession();
return accessToken.getJwtToken();
}
async function signIn() {
try {
const user = await Auth.signIn('username', 'password');
console.log(user);
} catch (error) {
console.log('error signing in', error);
}
}
async function list_users() {
const { data } = await axios.get(
'url',
{
headers: {
'Authorization': `${await get_access_token()}`
}
}
)
return data;
}
export let name;
let data = [];
onMount(async () => {
await signIn();
data = await list_users();
});
</script>
<main>
<h1>Hello {name}!</h1>
<div>
{#each data as result}
<p>{result.name}</p>
{/each}
</div>
</main>
<style>
main {
text-align: center;
padding: 1em;
max-width: 240px;
margin: 0 auto;
}
h1 {
color: #ff3e00;
text-transform: uppercase;
font-size: 4em;
font-weight: 100;
}
@media (min-width: 640px) {
main {
max-width: none;
}
}
</style>
import svelte from 'rollup-plugin-svelte';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import json from "@rollup/plugin-json"; // new!
import { terser } from 'rollup-plugin-terser';
const production = !process.env.ROLLUP_WATCH;
function serve() {
let server;
function toExit() {
if (server) server.kill(0);
}
return {
writeBundle() {
if (server) return;
server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
stdio: ['ignore', 'inherit', 'inherit'],
shell: true
});
process.on('SIGTERM', toExit);
process.on('exit', toExit);
}
};
}
export default {
input: 'src/main.js',
output: {
sourcemap: true,
format: 'iife',
name: 'app',
file: 'public/build/bundle.js'
},
plugins: [
svelte({
// enable run-time checks when not in production
dev: !production,
// we'll extract any component CSS out into
// a separate file - better for performance
css: css => {
css.write('bundle.css');
}
}),
// If you have external dependencies installed from
// npm, you'll most likely need these plugins. In
// some cases you'll need additional configuration -
// consult the documentation for details:
// https://github.com/rollup/plugins/tree/master/packages/commonjs
resolve({
browser: true,
preferBuiltins: false,
dedupe: ['svelte']
}),
commonjs(),
json(),
// In dev mode, call `npm run start` once
// the bundle has been generated
!production && serve(),
// Watch the `public` directory and refresh the
// browser on changes when not in production
!production && livereload('public'),
// If we're building for production (npm run build
// instead of npm run dev), minify
production && terser()
],
watch: {
clearScreen: false
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment